Spring控制反转(IoC)容器

开发 后端
本文介绍Spring IoC容器,以及介绍BeanFactory 接口,ApplicationContext接口和配置元数据。

个人整理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的配置元数据时,将在顶层的<beans/>元素中配置一个或多个<bean/>元素。      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方法。

  1. HelloBean.java  
  2. view plaincopy to clipboardprint?  
  3. <FONT size=2>  package com.qu.bean;  
  4.   public class HelloBean {  
  5.    public String sayHello(String name){  
  6.    return String.format("%1$s : Hello World!", name);  
  7.    }  
  8.   }</FONT> 
  9.   package com.qu.bean;  
  10.   public class HelloBean {  
  11.    public String sayHello(String name){  
  12.    return String.format("%1$s : Hello World!", name);  
  13.    }  
  14.   } 

4.配置applicationContext.xml 将HelloBean注入Spring容器。

  1. applicationContext.xml  
  2. view plaincopy to clipboardprint?  
  3. <FONT size=2>  <?xml version="1.0" encoding="UTF-8"?> 
  4.   <beans 
  5.    xmlns="http://www.springframework.org/schema/beans" 
  6.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  7.    xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
  8.   <!--方法2  
  9.    <import resource="OtherXML/helloBean.xml"/> 
  10.   --> 
  11.   <!--方法1--> 
  12.      <bean class="com.qu.bean.HelloBean" id="helloBean"> 
  13.    </bean> 
  14.   </beans></FONT> 
  15.   <?xml version="1.0" encoding="UTF-8"?> 
  16.   <beans 
  17.    xmlns="http://www.springframework.org/schema/beans" 
  18.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  19.    xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
  20.   <!--方法2  
  21.    <import resource="OtherXML/helloBean.xml"/> 
  22.   --> 
  23.   <!--方法1--> 
  24.      <bean class="com.qu.bean.HelloBean" id="helloBean"> 
  25.    </bean> 
  26.   </beans>view plaincopy to clipboardprint?  
  27. <FONT size=2><STRONG><U>helloBean.xml</U></STRONG></FONT> 
  28. helloBean.xmlview plaincopy to clipboardprint?  
  29. <?xml version="1.0" encoding="UTF-8"?> 
  30. <beans 
  31.                      xmlns="http://www.springframework.org/schema/beans" 
  32.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  33.     xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
  34.     <bean class="com.qu.bean.HelloBean" id="helloBean"> 
  35.     </bean> 
  36. </beans> 
  37. <?xml version="1.0" encoding="UTF-8"?> 
  38. <beans 
  39.                      xmlns="http://www.springframework.org/schema/beans" 
  40.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  41.  xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
  42.  <bean class="com.qu.bean.HelloBean" id="helloBean"> 
  43.  </bean> 
  44. </beans>view plaincopy to clipboardprint?  
  45. <FONT size=2></FONT> 

5.导入Junit 4 测试。

6.编写测试类TestHello 。重写setUp方法实例化容器,编写testHello方法测试HelloBean的hello方法。

  1. view plaincopy to clipboardprint?  
  2.     <FONT size=2>   TestHello.java</FONT> 
  3.        TestHello.javaview plaincopy to clipboardprint?  
  4.     <FONT size=2> 
  5.        package com.qu.test;  
  6.        import org.springframework.context.ApplicationContext;  
  7.        import org.springframework.context.support.ClassPathXmlApplicationContext;  
  8.        import com.qu.bean.HelloBean;  
  9.        import junit.framework.TestCase;  
  10.        public class TestHello extends TestCase {  
  11.         private ApplicationContext ctx;  
  12.         private HelloBean hello;  
  13.         protected void setUp() throws Exception {  
  14.          super.setUp();  
  15.          this.ctx = new ClassPathXmlApplicationContext(  
  16.            new String[] {"ApplicationContext.xml","OtherXML/helloBean.xml"});  
  17.          this.hello = (HelloBean) this.ctx.getBean("helloBean");  
  18.         }  
  19.         public void testSayHello(){  
  20.          assertEquals("Java : Hello World!", this.hello.sayHello("Java"));  
  21.         }  
  22.        }  
  23.     </FONT> 

【编辑推荐】

  1. 当Spring遇到Hibernate的时候
  2. 将Flex与Spring集成框架
  3. 如何集成Struts和Spring
  4. Spring2.0升级Spring2.0.7的变化
  5. Spring 2.0新功能
责任编辑:佚名 来源: 中国IT实验室
相关推荐

2022-04-30 08:50:11

控制反转Spring依赖注入

2012-07-02 15:26:19

Spring架构框架

2024-03-14 10:47:12

Spring生命周期阿里

2023-03-20 13:41:00

IoC容器Spring

2022-03-16 11:11:37

SpringBean项目

2009-06-18 13:31:03

Spring工作原理

2020-07-14 14:59:00

控制反转依赖注入容器

2020-09-03 11:04:20

Spring 循环依赖

2021-01-06 08:34:21

Spring核心组件

2022-08-10 07:06:57

IoCDISpring

2012-07-17 09:16:16

SpringSSH

2017-08-16 16:00:05

PHPcontainer依赖注入

2019-09-18 18:12:57

前端javascriptvue.js

2021-04-29 07:18:21

Spring IOC容器单例

2012-02-02 13:04:50

JavaSpring

2022-12-07 08:02:43

Spring流程IOC

2021-05-06 07:58:57

Spring BeanIOCAOP

2024-03-13 15:41:03

Spring设计IOC

2021-03-10 05:50:06

IOCReact解耦组件

2021-05-07 21:32:51

SpringIOC分析
点赞
收藏

51CTO技术栈公众号