自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring控制反轉(zhuǎn)(IoC)容器

開發(fā) 后端
本文介紹Spring IoC容器,以及介紹BeanFactory 接口,ApplicationContext接口和配置元數(shù)據(jù)。

個(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方法。

  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.導(dǎo)入Junit 4 測(cè)試。

6.編寫測(cè)試類TestHello 。重寫setUp方法實(shí)例化容器,編寫testHello方法測(cè)試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. 當(dāng)Spring遇到Hibernate的時(shí)候
  2. 將Flex與Spring集成框架
  3. 如何集成Struts和Spring
  4. Spring2.0升級(jí)Spring2.0.7的變化
  5. Spring 2.0新功能
責(zé)任編輯:佚名 來源: 中國(guó)IT實(shí)驗(yàn)室
相關(guān)推薦

2024-05-10 07:19:46

IOC依賴倒置控制反轉(zhuǎn)

2022-04-30 08:50:11

控制反轉(zhuǎn)Spring依賴注入

2012-07-02 15:26:19

Spring架構(gòu)框架

2024-03-14 10:47:12

Spring生命周期阿里

2023-03-20 13:41:00

IoC容器Spring

2025-03-14 10:37:24

SpringSpring IOC容器

2022-03-16 11:11:37

SpringBean項(xiàng)目

2009-06-18 13:31:03

Spring工作原理

2020-07-14 14:59:00

控制反轉(zhuǎn)依賴注入容器

2020-09-03 11:04:20

Spring 循環(huán)依賴

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

2012-02-02 13:04:50

JavaSpring

2022-12-07 08:02:43

Spring流程IOC

2021-04-29 07:18:21

Spring IOC容器單例

2021-05-06 07:58:57

Spring BeanIOCAOP

2024-03-13 15:41:03

Spring設(shè)計(jì)IOC
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)