---------------siwuxie095

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

创建一个基于
Spring IoC
的小程序的步骤:

 
 

  • 建立
    Spring
    工程
  • 编写
    Java
    文件
  • 编写配置文件
  • 运行示例工程

     
     

     
     

     
     

    示例:

     
     

    一个人,在中国时用中文问候大家;在外国时,用英语问候大家

     
     

    人的具体位置,由
    Spring
    的配置环境来决定的:

    ·
    当配置为中国时,则问候:"大家好"

    ·
    当配置为外国时,则问候:"Hello everybody"

     
     

     
     

     
     

     
     

     
     

    工程名:TestHelloMessage

    包名:com.siwuxie095.spring

    类名:HelloMessage.java、HelloChina.java、HelloWorld.java、

    Person.java、Main.java(主类)

     
     

     
     

    打开资源管理器,在工程 TestHelloMessage 文件夹下,创建一个

    文件夹:lib,在其中放入三个必需的 jar 包:

    (1)spring-core-4.3.7.RELEASE.jar

    (2)spring-beans-4.3.7.RELEASE.jar

    (3)commons-logging-1.2.jar

     
     

     
     

    选择三个
    jar 包,右键->Build Path->Add to Build Path

     
     

    工程结构目录如下:

     
     

     
     

     
     

     
     

     
     

    HelloMessage.java:

     
     

package com.siwuxie095.spring;

 
 

//接口 HelloMessage,用于定义输出问候信息

public interface HelloMessage {

/**

* 方法的简写,因为接口中只允许存在抽象方法和全局常量

* 所以 public 和 abstract 可以省略掉
或只省略 abstract

*/

public String sayHello();

 
 

}

 
 

 
 

 
 

HelloChina.java:

 
 

package com.siwuxie095.spring;

 
 

// HelloChina 是接口的实现类,用于输出
大家好

public class HelloChina implements HelloMessage {

 
 

@Override

public String sayHello() {

return
"大家好";

}

 
 

}

 
 

 
 

 
 

HelloWorld.java:

 
 

package com.siwuxie095.spring;

 
 

// HelloWorld 是接口的实现类,用于输出 Hello everybody

public class HelloWorld implements HelloMessage {

 
 

@Override

public String sayHello() {

return
"Hello everybody";

}

 
 

}

 
 

 
 

 
 

Person.java:

 
 

package com.siwuxie095.spring;

 
 

//Person类,用于调用 HelloMessage 接口,输出问候信息

public class Person {

 

//将HelloMessage作为一个属性,用于向大家输出问候信息

private HelloMessage helloMessage;

 
 

public HelloMessage getHelloMessage() {

return helloMessage;

}

 
 

public
void setHelloMessage(HelloMessage helloMessage) {

this.helloMessage = helloMessage;

}

 

/**

* 用于调用HelloMessage接口向用户输出问候信息,

* 具体的问候信息,由Spring的配置文件来分配和决定:

* 1.当配置文件中分配给person的是HelloChina的实体时,则输出"大家好"的信息

* 2.当配置文件中分配给person的是HelloWorld的实体时,则输出"Hello everybody"的信息

*/

public String sayHello() {

return this.helloMessage.sayHello();

}

 

 
 

}

 
 

 
 

 
 

Main.java(主类):

 
 

package com.siwuxie095.spring;

 
 

import org.springframework.beans.factory.support.DefaultListableBeanFactory;

import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

import org.springframework.core.io.FileSystemResource;

import org.springframework.core.io.Resource;

 
 

public class Main {

 
 

/**

* 代码段(1)和(2)等效

* 因为 Spring 3.1 之后已经废弃 XmlBeanFactory

* 所以这里注释掉(2),使用(1)

* 但(2)依然可用,导入相关包和类即可

*/

public static
void main(String[] args) {

 

//(1)

//使用 FileSystemResource 来读取配置文件

Resource resource=new FileSystemResource("helloMessage.xml");

DefaultListableBeanFactory factory=new DefaultListableBeanFactory();

XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(factory);

reader.loadBeanDefinitions(resource);

//从IOC容器中获取Person类的实例

//Person person=(Person)b.getBean("Person");

Person person= (Person) factory.getBean("person");

//使用person类示例来输出问候信息

String str=person.sayHello();

System.out.println("The Person is currently saying:"+str);

 

 

// //(2)

// //使用 FileSystemResource() 读取配置文件 helloMessage.xml

// Resource resource=new FileSystemResource("helloMessage.xml");

// //使用 XmlBeanFactory() 加载配置文件,启动 IoC 容器

// BeanFactory factory=new XmlBeanFactory(resource);

// //从 IoC 容器中获取 Person 类的实例

// Person person=(Person) factory.getBean("person");

// String str=person.sayHello();

// System.out.println("The person is currently saying:"+str);

 

}

 
 

}

 
 

 
 

 
 

创建配置文件
helloMessage.xml:

 
 

<?xml
version="1.0"
encoding="UTF-8"?>

<beans
xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

 
 

 
 

<!-- id 和 class 是 bean 的属性,id 是唯一标识,class 是对应的类名(完全限定名) -->

<bean
id="helloChina"
class="com.siwuxie095.spring.HelloChina">

</bean>

<bean
id="helloWorld"
class="com.siwuxie095.spring.HelloWorld">

</bean>

 

<!-- 为 person 添加属性,该属性引用了bean:helloWorld -->

<!-- 此时,person 就和 helloWorld 建立了依赖关系 -->

<!-- 也可以引用 helloChina,只需要修改 ref 属性即可 -->

<!-- 它们之间的依赖关系,根据配置文件决定,具体配置谁,控制权由原来的对象本身转移到配置文件中 -->

<bean
id="person"
class="com.siwuxie095.spring.Person">

<property
name="helloMessage"
ref="helloWorld"></property>

</bean>

 
 

</beans>

 
 

 
 

此时,工程结构目录一览:

 
 

 
 

 
 

 
 

运行一览:

 
 

 
 

 
 

 
 

 
 

Spring Bean 和 JavaBean 的区别:

 
 

参考链接1参考链接2参考链接3参考链接4

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

【made by siwuxie095】

Spring示例工程的更多相关文章

  1. 使用方法拦截机制在不修改原逻辑基础上为 spring MVC 工程添加 Redis 缓存

    首先,相关文件:链接: https://pan.baidu.com/s/1H-D2M4RfXWnKzNLmsbqiQQ 密码: 5dzk 文件说明: redis-2.4.5-win32-win64.z ...

  2. Ubuntu下使用VS Code创建Spring Boot工程

    目的 我们将在Ubuntu桌面系统下,使用VS Code(Visual Studio Code)编辑器从零开始创建一个Spring Boot工程,并实现一个简单的RESTful风格接口.使用这套流程的 ...

  3. Spring Boot工程发布到Docker

    先聊聊闲话 搞过企业级的application运维的同仁肯定深有感触,每个application的功能交叉错杂,数据交换就让人焦头烂额(当然这和顶层业务设计有关系), 几十个application发布 ...

  4. 【AT91SAM3S】英倍特串口示例工程05-UART中,串口是怎样初始化的

    在这个示例工程的main.c文件中,进入main之后,没有发现串口功能的任何配置.直接使用了printf这个东西进行输出.将软件下载到开发板上之后,在电脑端使用串口软件,可以看板子有数据发来.说明这个 ...

  5. Springboot(一):使用Intellij中的Spring Initializr来快速构建Spring Boot工程

    使用Intellij中的Spring Initializr来快速构建Spring Boot工程 New---Project 可以看到图所示的创建功能窗口.其中Initial Service Url指向 ...

  6. spring cloud 工程构建

    https://blog.csdn.net/zhou199252/article/details/80745151 https://blog.csdn.net/forezp/article/detai ...

  7. SpringCloud核心教程 | 第一篇: 使用Intellij中的Spring Initializr来快速构建Spring Cloud工程

    spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...

  8. SpringCloud核心教程 | 第二篇: 使用Intellij中的maven来快速构建Spring Cloud工程

    spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...

  9. activiti学习2:示例工程activiti-explorer.war的使用

    目录 activiti学习2:示例工程activiti-explorer.war的使用 一.搭建开发环境 二.运行示例工程 三.示例工程功能演示 1. 创建流程图 2. 部署流程图 3. 启动流程 4 ...

随机推荐

  1. 加深Java基础,做了20道题选择题!简答题没做

    2015-03-16 17:13 269人阅读 评论(1) 收藏 举报  分类: 笔试(1)  版权声明:本文为博主原创文章,未经博主允许不得转载.    1,下列说法正确的是( A ) A )Jav ...

  2. 适用grunt的注意点

    0.使用grunt可以为前端开发省去很多工作量,与git版本控制器配合起来不要太完美,一般也都是这么用的: 1.先安装node.js,下载软件安装就行了,一般自带npm管理器; 2.通过npm安装gr ...

  3. Charles进行HTTPS抓包(iOS为例)

    各种抓包工具的原理都是一样的,使用方面也都是差不多的,因为最近在用Mac,所以抓包工具开始用Charles了,记录一下抓取HTTPS的步骤. 连接代理, 开启抓包工具, 手机设置代理服务器,端口号(默 ...

  4. UNIDBgrid里动态添加clientevents实现回车替换TAB

    //GRID里回车替换TABfunction cellkeydown(sender, td, cellIndex, record, tr, rowIndex, e, eOpts){ if (e.get ...

  5. 织梦dedecms 调用文章图片数功能

    function BodyImgNum($aid) { global $dsql; $sql = "select aid,body from dede_addonarticle where ...

  6. 关于ansible变量的一个问题

    ansible-playbook 使用with_items 时 items中 如果有变量 {} 外面可以用 “” items中 如果都是固定值,没有用到变量,{}最外面不要加 “” ,不然报错,mmp

  7. bootstrap0

    bootstrap模板为使IE6.7.8版本(IE9以下版本)浏览器兼容html5新增的标签,引入下面代码文件即可. <script src="https://oss.maxcdn.c ...

  8. matlab中的科学记数法变成小数形式

    例如:假如rectx的形式在命令窗口显示: rectx = 1.0e+05 * 5.2294 5.2294 5.2294 5.2294 5.2294 那么,命令窗口输入vpa(rectx): ans ...

  9. 常用连续型分布介绍及R语言实现

    常用连续型分布介绍及R语言实现 R的极客理想系列文章,涵盖了R的思想,使用,工具,创新等的一系列要点,以我个人的学习和体验去诠释R的强大. R语言作为统计学一门语言,一直在小众领域闪耀着光芒.直到大数 ...

  10. for循环中删除map中的元素,valgrind检测提示error:Invalid read of size 8

    #include <iostream> #include <map> using namespace std; class A { public: typedef std::m ...