成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

個人整理Spring系列:控制反轉(zhuǎn)(IoC)容器

一.什么是控制反轉(zhuǎn)模式?  不創(chuàng)建對象,但是描述創(chuàng)建它們的方式。在代碼中不直接與對象和服務連接,但在配置文件中描述哪一個組件需要哪一項服務。  容器 (在 Spring 框架中是 IOC 容器) 負責將這些聯(lián)系在一起。

二.Spring 中的 Bean?  由Spring IoC容器所管理的對象被稱之為bean。bean就是由Spring容器初始化、裝配及被管理的對象。  bean定義以及bean相互間的依賴關(guān)系將通過配置元數(shù)據(jù)來描述。   三,什么是Spring IoC容器?  org.springframework.beans包是Spring IoC容器的基礎。  org.springframework.beans.factory.BeanFactory接口是Spring IoC容器的實際代表者。  IoC容器負責容納此前所描述的bean,并對bean進行管理。

1.BeanFactory 接口   BeanFactory是IoC容器的核心接口。是工廠設計模式的實現(xiàn)。bean 工廠的概念是 Spring 作為 IOC 容器的基礎。   它的職責包括:實例化、檢索、配置應用程序中的對象及管理對象之間的關(guān)系。      BeanFactory 支持兩個對象模型。    單態(tài)模型:提供了具有特定名稱的對象的共享實例,可以在查詢時對其進行檢索。Singleton 是默認的也是最常用的對象模型。對于無狀態(tài)服務對象很理想。    原型模型:確保每次檢索都會創(chuàng)建單獨的對象。在每個用戶都需要自己的對象時,原型模型最適合。

2.ApplicationContext接口   org.springframework.context.ApplicationContext由BeanFactory接口派生擴展而來,因而提供了 BeanFactory所有的功能。   在構(gòu)建J2EE應用時,使用ApplicationContext將是更好的選擇。      context包還提供了以下的功能:    MessageSource, 提供國際化的消息訪問。    資源訪問,如URL和文件。    事件傳播,實現(xiàn)了ApplicationListener接口的bean。    載入多個(有繼承關(guān)系)上下文 。

3.配置元數(shù)據(jù)   Spring IoC容器將讀取配置元數(shù)據(jù);并通過它對應用中各個對象進行實例化、配置以及組裝。      基于XML的元數(shù)據(jù)是最常用到的配置元數(shù)據(jù)格式。然而,它并不是***的描述格式。Spring IoC容器在這一點上是完全開放的。   當使用基于XML的配置元數(shù)據(jù)時,將在頂層的<beans/>元素中配置一個或多個<bean/>元素。      bean定義與應用程序中實際使用的對象一一對應。通常情況下bean的定義包括: 服務層對象、數(shù)據(jù)訪問層對象(DAO)、類似Struts Action的表示層對象、Hibernate SessionFactory對象、JMS Queue對象等等。

四.實例化IoC容器(基于XML的元數(shù)據(jù))  通過ClassPathXmlApplicationContext類加載一個或多個XML文檔來實例化BeanFactory接口的實現(xiàn)擴展 ApplicationContext類。  要從 BeanFactory 檢索 bean,只需調(diào)用 getBean() 方法,傳入將要檢索的 bean 的名稱即可。

五.一個簡單Spring 示例

1.建立Java項目:MySpring

2.導入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.導入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實驗室
相關(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生命周期阿里

2022-03-16 11:11:37

SpringBean項目

2025-03-14 10:37:24

SpringSpring IOC容器

2025-05-21 10:09:09

Spring 5.xIOC編程

2023-03-20 13:41:00

IoC容器Spring

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

2021-04-29 07:18:21

Spring IOC容器單例

2022-12-07 08:02:43

Spring流程IOC

2012-02-02 13:04:50

JavaSpring

2024-03-13 15:41:03

Spring設計IOC
點贊
收藏

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

主站蜘蛛池模板: 国产成人午夜电影网 | 日韩理论电影在线观看 | 一区二区精品视频 | 亚洲精品4| 日韩五月天 | 日本偷偷操 | 欧美一级久久久猛烈a大片 日韩av免费在线观看 | 午夜理伦三级理论三级在线观看 | 欧美一级片在线看 | 最新伦理片 | 欧美h版 | 中文字幕日本一区二区 | 高清久久 | 日本一卡精品视频免费 | 美女视频一区 | 日韩成人在线观看 | 成人精品啪啪欧美成 | 精品久久久久一区二区国产 | 久久综合久久综合久久综合 | 国产一级电影在线 | 亚洲一区免费视频 | 99久久久久久99国产精品免 | 国产在线精品一区二区 | 亚洲精品中文字幕在线观看 | 欧美日韩最新 | 精品九九久久 | 日韩欧美一区在线 | 国产精品色哟哟网站 | 欧美成人精品一区二区男人看 | 午夜影院在线视频 | 中文字幕成人av | 在线观看免费高清av | 欧美一级特黄aaa大片在线观看 | 欧美日韩精品一区二区三区四区 | 日韩在线免费视频 | 国产激情在线 | 久久久久久久国产精品 | 欧美五月婷婷 | 一区二区精品在线 | 久久久精品一区 | 亚洲伊人精品酒店 |