溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring如何控制反轉(zhuǎn)IOC容器

發(fā)布時(shí)間:2021-10-27 09:28:47 來源:億速云 閱讀:153 作者:柒染 欄目:編程語言

本篇文章給大家分享的是有關(guān)Spring如何控制反轉(zhuǎn)IOC容器,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

控制反轉(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, 提供國際化的消息訪問。    資源訪問,如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> 

以上就是Spring如何控制反轉(zhuǎn)IOC容器,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI