Spring控制反轉(zhuǎn)(IoC)容器
個(gè)人整理Spring系列:控制反轉(zhuǎn)(IoC)容器
一.什么是控制反轉(zhuǎn)模式? 不創(chuàng)建對(duì)象,但是描述創(chuàng)建它們的方式。在代碼中不直接與對(duì)象和服務(wù)連接,但在配置文件中描述哪一個(gè)組件需要哪一項(xiàng)服務(wù)。 容器 (在 Spring 框架中是 IOC 容器) 負(fù)責(zé)將這些聯(lián)系在一起。
二.Spring 中的 Bean? 由Spring IoC容器所管理的對(duì)象被稱之為bean。bean就是由Spring容器初始化、裝配及被管理的對(duì)象。 bean定義以及bean相互間的依賴關(guān)系將通過配置元數(shù)據(jù)來描述。 三,什么是Spring IoC容器? org.springframework.beans包是Spring IoC容器的基礎(chǔ)。 org.springframework.beans.factory.BeanFactory接口是Spring IoC容器的實(shí)際代表者。 IoC容器負(fù)責(zé)容納此前所描述的bean,并對(duì)bean進(jìn)行管理。
1.BeanFactory 接口 BeanFactory是IoC容器的核心接口。是工廠設(shè)計(jì)模式的實(shí)現(xiàn)。bean 工廠的概念是 Spring 作為 IOC 容器的基礎(chǔ)。 它的職責(zé)包括:實(shí)例化、檢索、配置應(yīng)用程序中的對(duì)象及管理對(duì)象之間的關(guān)系。 BeanFactory 支持兩個(gè)對(duì)象模型。 單態(tài)模型:提供了具有特定名稱的對(duì)象的共享實(shí)例,可以在查詢時(shí)對(duì)其進(jìn)行檢索。Singleton 是默認(rèn)的也是最常用的對(duì)象模型。對(duì)于無狀態(tài)服務(wù)對(duì)象很理想。 原型模型:確保每次檢索都會(huì)創(chuàng)建單獨(dú)的對(duì)象。在每個(gè)用戶都需要自己的對(duì)象時(shí),原型模型最適合。
2.ApplicationContext接口 org.springframework.context.ApplicationContext由BeanFactory接口派生擴(kuò)展而來,因而提供了 BeanFactory所有的功能。 在構(gòu)建J2EE應(yīng)用時(shí),使用ApplicationContext將是更好的選擇。 context包還提供了以下的功能: MessageSource, 提供國(guó)際化的消息訪問。 資源訪問,如URL和文件。 事件傳播,實(shí)現(xiàn)了ApplicationListener接口的bean。 載入多個(gè)(有繼承關(guān)系)上下文 。
3.配置元數(shù)據(jù) Spring IoC容器將讀取配置元數(shù)據(jù);并通過它對(duì)應(yīng)用中各個(gè)對(duì)象進(jìn)行實(shí)例化、配置以及組裝。 基于XML的元數(shù)據(jù)是最常用到的配置元數(shù)據(jù)格式。然而,它并不是***的描述格式。Spring IoC容器在這一點(diǎn)上是完全開放的。 當(dāng)使用基于XML的配置元數(shù)據(jù)時(shí),將在頂層的<beans/>元素中配置一個(gè)或多個(gè)<bean/>元素。 bean定義與應(yīng)用程序中實(shí)際使用的對(duì)象一一對(duì)應(yīng)。通常情況下bean的定義包括: 服務(wù)層對(duì)象、數(shù)據(jù)訪問層對(duì)象(DAO)、類似Struts Action的表示層對(duì)象、Hibernate SessionFactory對(duì)象、JMS Queue對(duì)象等等。
四.實(shí)例化IoC容器(基于XML的元數(shù)據(jù)) 通過ClassPathXmlApplicationContext類加載一個(gè)或多個(gè)XML文檔來實(shí)例化BeanFactory接口的實(shí)現(xiàn)擴(kuò)展 ApplicationContext類。 要從 BeanFactory 檢索 bean,只需調(diào)用 getBean() 方法,傳入將要檢索的 bean 的名稱即可。
五.一個(gè)簡(jiǎn)單Spring 示例
1.建立Java項(xiàng)目:MySpring
2.導(dǎo)入Spring框架。
3.創(chuàng)建JavaBean:HelloBean。編寫testHello方法。
- HelloBean.java
- view plaincopy to clipboardprint?
- <FONT size=2> package com.qu.bean;
- public class HelloBean {
- public String sayHello(String name){
- return String.format("%1$s : Hello World!", name);
- }
- }</FONT>
- 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?
- <FONT size=2> <?xml version="1.0" encoding="UTF-8"?>
- <beans
- 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">
- <!--方法2
- <import resource="OtherXML/helloBean.xml"/>
- -->
- <!--方法1-->
- <bean class="com.qu.bean.HelloBean" id="helloBean">
- </bean>
- </beans></FONT>
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- 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">
- <!--方法2
- <import resource="OtherXML/helloBean.xml"/>
- -->
- <!--方法1-->
- <bean class="com.qu.bean.HelloBean" id="helloBean">
- </bean>
- </beans>view plaincopy to clipboardprint?
- <FONT size=2><STRONG><U>helloBean.xml</U></STRONG></FONT>
- helloBean.xmlview plaincopy to clipboardprint?
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- 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">
- <bean class="com.qu.bean.HelloBean" id="helloBean">
- </bean>
- </beans>
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- 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">
- <bean class="com.qu.bean.HelloBean" id="helloBean">
- </bean>
- </beans>view plaincopy to clipboardprint?
- <FONT size=2></FONT>
5.導(dǎo)入Junit 4 測(cè)試。
6.編寫測(cè)試類TestHello 。重寫setUp方法實(shí)例化容器,編寫testHello方法測(cè)試HelloBean的hello方法。
- view plaincopy to clipboardprint?
- <FONT size=2> TestHello.java</FONT>
- TestHello.javaview plaincopy to clipboardprint?
- <FONT 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"));
- }
- }
- </FONT>
【編輯推薦】