Spring 小示例

通过一个简单的示例来初步理解Spring框架

1、创建java工程,导入相应Spring包,放在lib文件夹中

2、接口  IHelloMessage

1 package com.jike.spring.chapter01;
2 
3 public interface IHelloMessage {
4     
5     //接口中的一个抽象方法,用于向大家输出问候信息
6     public String sayHello();
7 
8 }

3、java类 HelloChina.java,用于输出“中国话”

 1 package com.jike.spring.chapter01;
 2 
 3 public class HelloChina implements IHelloMessage {
 4 
 5     
 6     //继承和实现了IHelloMessage接口中的sayHello方法,
 7     //向大家输出了Hello China的信息
 8     @Override
 9     public String sayHello() {
10 
11         return "Hello China!";
12     }
13 
14 }

4、java类 HelloWorld.java,用于输出“世界话”

 1 package com.jike.spring.chapter01;
 2 
 3 public class HelloWorld implements IHelloMessage{
 4     
 5     //继承和实现了IHelloMessage接口中的sayHello方法,
 6     //向大家输出了Hello China的信息
 7     @Override
 8     public String sayHello() {
 9         return "Hello World!";
10     }
11 
12 }

5、Person类

 1 package com.jike.spring.chapter01;
 2 
 3 public class Person {
 4 
 5     //将IHelloMessage作为一个属性,用于向大家输出问候信息
 6     private IHelloMessage helloMessage;
 7 
 8     
 9     public IHelloMessage getHelloMessage() {
10         return helloMessage;
11     }
12 
13     public void setHelloMessage(IHelloMessage helloMessage) {
14         this.helloMessage = helloMessage;
15     }
16     
17     //用于调用IHelloMessage接口向用户输出问候信息,
18     //具体的问候信息,由Spring的配置文件来分配和决定
19     //1.当配置文件中分配给person的是HelloWorld的实体时,则输出“Hello World!”的信息;
20     //2.当配置文件中分配给person的是HelloChina的实体时,则输出“Hello China!”的信息;
21     public String sayHello() {
22         return this.helloMessage.sayHello();
23     }
24 }

6、Main主类

 1 package com.jike.spring.chapter01;
 2 
 3 import org.springframework.beans.factory.BeanFactory;
 4 import org.springframework.beans.factory.xml.XmlBeanFactory;
 5 import org.springframework.core.io.FileSystemResource;
 6 import org.springframework.core.io.Resource;
 7 
 8 public class Main {
 9 
10     public static void main(String[] args) {
11 
12         //利用FileSystemResource读取配置文件
13         Resource r = new FileSystemResource("helloMessage.xml");
14         //利用XmlBeanFactory来加载配置文件,启动IOC容器
15         BeanFactory f = new XmlBeanFactory(r);
16         //从IOC容器中获取Person类的实例
17         Person person = (Person) f.getBean("person");
18         //person实例向大家输出问候信息
19         String s = person.sayHello();
20         //在系统控制台中打印问候信息,由于在这里配置文件中配置是HelloWorld的实例,
21         //所以,在这里打印的是字符串:HelloWorld
22         System.out.println("The Person is currently saying: "+s);
23     }
24 
25 }

7、配置helloMessage.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
 3 "http://www.springframework.org/dtd/spring-beans.dtd">
 4 <beans>
 5     <bean id="helloWorld" class="com.jike.spring.chapter01.HelloWorld"></bean>
 6     <bean id="helloChina" class="com.jike.spring.chapter01.HelloChina"></bean>
 7     <bean id="person" class="com.jike.spring.chapter01.Person">
 8         <property name="helloMessage" ref="helloChina"/>
 9     </bean>
10 </beans>

运行结果:

将配置文件改为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" 
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="helloWorld" class="com.jike.spring.chapter01.HelloWorld"></bean>
    <bean id="helloChina" class="com.jike.spring.chapter01.HelloChina"></bean>
    <bean id="person" class="com.jike.spring.chapter01.Person">
        <property name="helloMessage" ref="helloWorld"/>
    </bean>
</beans>

结果为:

原文地址:https://www.cnblogs.com/UniqueColor/p/5664648.html