新聞中心
個(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í),將在頂層的
四.實(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?
- 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.導(dǎo)入Junit 4 測(cè)試。
6.編寫測(cè)試類TestHello 。重寫setUp方法實(shí)例化容器,編寫testHello方法測(cè)試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"));
- }
- }
【編輯推薦】
- 當(dāng)Spring遇到Hibernate的時(shí)候
- 將Flex與Spring集成框架
- 如何集成Struts和Spring
- Spring2.0升級(jí)Spring2.0.7的變化
- Spring 2.0新功能
標(biāo)題名稱:Spring控制反轉(zhuǎn)(IoC)容器
本文URL:http://www.fisionsoft.com.cn/article/ccehhhj.html


咨詢
建站咨詢
