创建一个基于 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

工程结构目录如下:

spring项目代码结构推荐 spring项目实例_xml


HelloMessage.java:

package com.siwuxie095.spring;
   
//接口 HelloMessage,用于定义输出问候信息 
public interface HelloMessage {
/**
 * 方法的简写,因为接口中只允许存在抽象方法和全局常量  
 * 所以 public 和 abstract 可以省略掉 或只省略 abstract
 */ 
public String sayHello();
   
}


   

   

   

HelloChina.java:

   


package com.siwuxie095.spring;
   
// HelloChina 是接口的实现类,用于输出 大家好 
public classimplements HelloMessage {
   
@Override
public String sayHello() {
return"大家好"; 
 } 
   
}


   

   

   

HelloWorld.java:

   


package com.siwuxie095.spring;
   
// HelloWorld 是接口的实现类,用于输出 Hello everybody 
public classimplements 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;
 } 
   
publicvoid 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 .FileSystemResource;
import .Resource;
   
public class Main {
   
/**
 * 代码段(1)和(2)等效 
 * 因为 Spring 3.1 之后已经废弃 XmlBeanFactory
 * 所以这里注释掉(2),使用(1)
 * 但(2)依然可用,导入相关包和类即可
 */ 
public staticvoid main(String[] args) {
  
//(1)
//使用 FileSystemResource 来读取配置文件  
new"helloMessage.xml"); 
new DefaultListableBeanFactory(); 
new XmlBeanDefinitionReader(factory); 
 reader.loadBeanDefinitions(resource);  
//从IOC容器中获取Person类的实例  
//Person person=(Person)b.getBean("Person"); 
"person"); 
//使用person类示例来输出问候信息  
 String str=person.sayHello();  
"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:

   


<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">
   
   
<!-- id 和 class 是 bean 的属性,id 是唯一标识,class 是对应的类名(完全限定名) --> 
<beanid="helloChina"class="com.siwuxie095.spring.HelloChina">
</bean>
<beanid="helloWorld"class="com.siwuxie095.spring.HelloWorld">
</bean>
  
<!-- 为 person 添加属性,该属性引用了bean:helloWorld --> 
<!-- 此时,person 就和 helloWorld 建立了依赖关系 -->
<!-- 也可以引用 helloChina,只需要修改 ref 属性即可 -->
<!-- 它们之间的依赖关系,根据配置文件决定,具体配置谁,控制权由原来的对象本身转移到配置文件中 -->
<beanid="person"class="com.siwuxie095.spring.Person">
<propertyname="helloMessage"ref="helloWorld"></property>
</bean>
   
</beans>


   

   

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

   

spring项目代码结构推荐 spring项目实例_spring项目代码结构推荐_02


   

   

   

运行一览:

   

spring项目代码结构推荐 spring项目实例_java_03