Spring是一个类的容器实例化托管框架,可以实现对实现类的实例化进行动态的托管。更可以实现控制反转。控制反转就是应用本身不负责倚赖对象的创建和维护,倚赖对象的创建和维护是通过其他的外部容器负责的,这样的控制权就由应用转移到了容器。控制权的转移就是所谓的反转。

下面我们来说一下一个简单的Spring框架的搭建和实例.首先下载下来spring的压缩包,在解压后的dist文件夹下面有spring.jar和commons-logging.jar这两个JAR包,这就是实现最简单的spring框架所必须的包,然后就是在docs/reference下面的参考手册,里面又spring的配置文件写法。导入包完成后,最好在src目录下面建立beans.xml配置转就是应用本身不负责倚赖对象的创建和维护,倚赖对象的创建和维护是通过其他的外部容器负责的,这样的控制权就由应用转移到了容器。控制权的转移就是所谓的反转。

下面我们来说一下一个简单的Spring框架的搭建和实例.首先下载下来spring的压缩包,在解压后的dist文件夹下面有spring.jar和commons-logging.jar这两个JAR包,这就是实现最简单的spring框架所必须的包,然后就是在docs/reference下面的参考手册,里面又spring的配置文件写法。导入包完成后,最好在src目录下面建立beans.xml配置文件,这个名字是不限制的。

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">   
  6.       
  7.     <bean id="personService" class="com.bird.service.impl.PersonServerImpl"></bean>  
  8. </beans>  

然后写一个类和抽取出接口

  1. package com.bird.service;  
  2.   
  3. public interface PersonServer {  
  4.   
  5.     void save();  
  6.   
  7. }  
  8.   
  9. package com.bird.service.impl;  
  10.   
  11. import com.bird.service.PersonServer;  
  12.   
  13. public class PersonServerImpl implements PersonServer {  
  14.       
  15.     @Override  
  16.     public void save(){  
  17.         System.out.println("save()方法调用");  
  18.     }  
  19. }     

然后在beans.xml中配置这个类就可以使用spring实例化这个类了,下面的操作全部都是面向接口的编程了。

  1. package junit.test;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. import com.bird.service.PersonServer;  
  8.   
  9. public class SpringTest {  
  10.       
  11.     @Test  
  12.     public void test(){  
  13.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
  14.         PersonServer s = (PersonServer)ctx.getBean("personService");  
  15.         s.save();  
  16.     }                                                                                                                                                                                                                                                                
  17. }  

运行结果如下

  1. 2012-2-20 16:47:15 org.springframework.context.support.AbstractApplicationContext prepareRefresh  
  2. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@723d7c: display name [org.springframework.context.support.ClassPathXmlApplicationContext@723d7c]; startup date [Mon Feb 20 16:47:15 CST 2012]; root of context hierarchy  
  3. 2012-2-20 16:47:16 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
  4. 信息: Loading XML bean definitions from class path resource [beans.xml]  
  5. 2012-2-20 16:47:17 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory  
  6. 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@723d7c]: org.springframework.beans.factory.support.DefaultListableBeanFactory@a4e743  
  7. 2012-2-20 16:47:17 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons  
  8. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@a4e743: defining beans [personService]; root of factory hierarchy  
  9. save()方法调用