个人整理Spring系列:控制反转(IoC)容器

一.什么是控制反转模式?  不创建对象,但是描述创建它们的方式。在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务。  容器 (在 Spring 框架中是 IOC 容器) 负责将这些联系在一起。
二.Spring 中的 Bean?  由Spring IoC容器所管理的对象被称之为bean。bean就是由Spring容器初始化、装配及被管理的对象。  bean定义以及bean相互间的依赖关系将通过配置元数据来描述。   三,什么是Spring IoC容器?  org.springframework.beans包是Spring IoC容器的基础。  org.springframework.beans.factory.BeanFactory接口是Spring IoC容器的实际代表者。  IoC容器负责容纳此前所描述的bean,并对bean进行管理。
1.BeanFactory 接口   BeanFactory是IoC容器的核心接口。是工厂设计模式的实现。bean 工厂的概念是 Spring 作为 IOC 容器的基础。   它的职责包括:实例化、检索、配置应用程序中的对象及管理对象之间的关系。      BeanFactory 支持两个对象模型。    单态模型:提供了具有特定名称的对象的共享实例,可以在查询时对其进行检索。Singleton 是默认的也是最常用的对象模型。对于无状态服务对象很理想。    原型模型:确保每次检索都会创建单独的对象。在每个用户都需要自己的对象时,原型模型最适合。
2.ApplicationContext接口   org.springframework.context.ApplicationContext由BeanFactory接口派生扩展而来,因而提供了 BeanFactory所有的功能。   在构建J2EE应用时,使用ApplicationContext将是更好的选择。      context包还提供了以下的功能:    MessageSource, 提供国际化的消息访问。    资源访问,如URL和文件。    事件传播,实现了ApplicationListener接口的bean。    载入多个(有继承关系)上下文 。
3.配置元数据   Spring IoC容器将读取配置元数据;并通过它对应用中各个对象进行实例化、配置以及组装。      基于XML的元数据是最常用到的配置元数据格式。然而,它并不是***的描述格式。Spring IoC容器在这一点上是完全开放的。   当使用基于XML的配置元数据时,将在顶层的元素中配置一个或多个元素。      bean定义与应用程序中实际使用的对象一一对应。通常情况下bean的定义包括: 服务层对象、数据访问层对象(DAO)、类似Struts Action的表示层对象、Hibernate SessionFactory对象、JMS Queue对象等等。
四.实例化IoC容器(基于XML的元数据)  通过ClassPathXmlApplicationContext类加载一个或多个XML文档来实例化BeanFactory接口的实现扩展 ApplicationContext类。  要从 BeanFactory 检索 bean,只需调用 getBean() 方法,传入将要检索的 bean 的名称即可。
五.一个简单Spring 示例
1.建立Java项目:MySpring
2.导入Spring框架。
3.创建JavaBean:HelloBean。编写testHello方法。
- HelloBean.java   
- view plaincopy to clipboardprint?   
-  size=2>  package com.qu.bean;   
-   public class HelloBean {   
-    public String sayHello(String name){   
-    return String.format("%1$s : Hello World!", name);   
-    }   
-   }  
-   package com.qu.bean;   
-   public class HelloBean {   
-    public String sayHello(String name){   
-    return String.format("%1$s : Hello World!", name);   
-    }   
-   } 
4.配置applicationContext.xml 将HelloBean注入Spring容器。
- applicationContext.xml   
- view plaincopy to clipboardprint?   
-  size=2>   version="1.0" encoding="UTF-8"?>  
-     
-    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-2.0.xsd">  
-     
-       class="com.qu.bean.HelloBean" id="helloBean">  
-      
-     
-    version="1.0" encoding="UTF-8"?>  
-     
-    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-2.0.xsd">  
-     
-       class="com.qu.bean.HelloBean" id="helloBean">  
-      
-   view plaincopy to clipboardprint?   
-  size=2>helloBean.xml  
- helloBean.xmlview plaincopy to clipboardprint?   
-  version="1.0" encoding="UTF-8"?>  
-   
-                      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-2.0.xsd">  
-      class="com.qu.bean.HelloBean" id="helloBean">  
-       
-   
-  version="1.0" encoding="UTF-8"?>  
-   
-                      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-2.0.xsd">  
-   class="com.qu.bean.HelloBean" id="helloBean">  
-    
- view plaincopy to clipboardprint?   
-  size=2> 
5.导入Junit 4 测试。
6.编写测试类TestHello 。重写setUp方法实例化容器,编写testHello方法测试HelloBean的hello方法。
- view plaincopy to clipboardprint?   
-      size=2>   TestHello.java  
-        TestHello.javaview plaincopy to clipboardprint?   
-      size=2>  
-        package com.qu.test;   
-        import org.springframework.context.ApplicationContext;   
-        import org.springframework.context.support.ClassPathXmlApplicationContext;   
-        import com.qu.bean.HelloBean;   
-        import junit.framework.TestCase;   
-        public class TestHello extends TestCase {   
-         private ApplicationContext ctx;   
-         private HelloBean hello;   
-         protected void setUp() throws Exception {   
-          super.setUp();   
-          this.ctx = new ClassPathXmlApplicationContext(   
-            new String[] {"ApplicationContext.xml","OtherXML/helloBean.xml"});   
-          this.hello = (HelloBean) this.ctx.getBean("helloBean");   
-         }   
-         public void testSayHello(){   
-          assertEquals("Java : Hello World!", this.hello.sayHello("Java"));   
-         }   
-        }   
-      
【编辑推荐】
- 当Spring遇到Hibernate的时候
- 将Flex与Spring集成框架
- 如何集成Struts和Spring
- Spring2.0升级Spring2.0.7的变化
- Spring 2.0新功能
            本文名称:Spring控制反转(IoC)容器            
            分享路径:
http://www.zsjierui.cn/article/ccehhhj.html