<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 【第二章】 IoC 之 2.2 IoC 容器基本原理 ——跟我學Spring3 ### 2.2.1? IoC容器的概念 IoC容器就是具有依賴注入功能的容器,IoC容器負責實例化、定位、配置應用程序中的對象及建立這些對象間的依賴。應用程序無需直接在代碼中new相關的對象,應用程序由IoC容器進行組裝。在Spring中BeanFactory是IoC容器的實際代表者。 Spring IoC容器如何知道哪些是它管理的對象呢?這就需要配置文件,Spring IoC容器通過讀取配置文件中的配置元數據,通過元數據對應用中的各個對象進行實例化及裝配。一般使用基于xml配置文件進行配置元數據,而且Spring與配置文件完全解耦的,可以使用其他任何可能的方式進行配置元數據,比如注解、基于java文件的、基于屬性文件的配置都可以。 那Spring IoC容器管理的對象叫什么呢? ### 2.2.2? Bean的概念 由IoC容器管理的那些組成你應用程序的對象我們就叫它Bean, Bean就是由Spring_容器_初始化、裝配及管理的對象,除此之外,bean就與應用程序中的其他對象沒有什么區別了。那IoC怎樣確定如何實例化Bean、管理Bean之間的依賴關系以及管理Bean呢?這就需要配置元數據,在Spring中由BeanDefinition代表,后邊會詳細介紹,配置元數據指定如何實例化Bean、如何組裝Bean等。概念知道的差不多了,讓我們來做個簡單的例子。 ### 2.2.3? Hello World 一、配置環境: l???????**JDK安裝:**安裝最新的JDK,至少需要Java 1.5及以上環境; l???????**開發工具:**SpringSource Tool Suite,簡稱STS,是個基于Eclipse的開發環境,用以構建Spring應用,其最新版開始支持Spring 3.0及OSGi開發工具,但由于其太龐大,很多功能不是我們所必需的所以我們選擇Eclipse+ SpringSource Tool插件進行Spring應用開發;到eclipse官網下載最新的Eclipse,注意我們使用的是Eclipse IDE for Java EE Developers(eclipse-jee-helios-SR1); 安裝插件:啟動Eclipse,選擇Help-&gt;Install New Software,如圖2-3所示 ![](img/2fe87713b04c6b6916dc7fdae285cd2f__%E5%AE%89%E8%A3%850.jpg) ![](img/e26a64e589a7475cf2fed1bf3779e36a__%E5%AE%89%E8%A3%85.jpg) 圖2-3 安裝 2、首先安裝SpringSource Tool Suite插件依賴,如圖2-4: Name為:SpringSource Tool Suite Dependencies Location為:http://dist.springsource.com/release/TOOLS/composite/e3.6 ![](img/b7463593d18ea1206dada57934e62a1c__%E5%AE%89%E8%A3%853.jpg) 圖2-4 安裝 3、安裝SpringSource Tool Suite插件,只需安裝如圖2-5所選中的就可以: Name為:SpringSource Tool Suite Location為:http://dist.springsource.com/release/TOOLS/update/e3.6 ![](img/d894694934acfce8a4b8860de403507d__%E5%AE%89%E8%A3%854.jpg) 圖2-4 安裝 4、安裝完畢,開始項目搭建吧。 **Spring?依賴:**本書使用spring-framework-3.0.5.RELEASE spring-framework-3.0.5.RELEASE-with-docs.zip表示此壓縮包帶有文檔的; spring-framework-3.0.5.RELEASE-dependencies.zip表示此壓縮包中是spring的依賴jar包,所以需要什么依賴從這里找就好了; 下載地址:http://www.springsource.org/download 二、開始Spring Hello World之旅 1、準備需要的jar包 **核心jar包:**從下載的spring-framework-3.0.5.RELEASE-with-docs.zip中dist目錄查找如下jar包 ``` org.springframework.asm-3.0.5.RELEASE.jar org.springframework.core-3.0.5.RELEASE.jar org.springframework.beans-3.0.5.RELEASE.jar org.springframework.context-3.0.5.RELEASE.jar org.springframework.expression-3.0.5.RELEASE.jar ``` **依賴的jar包:**從下載的spring-framework-3.0.5.RELEASE-dependencies.zip中查找如下依賴jar包 ``` com.springsource.org.apache.log4j-1.2.15.jar com.springsource.org.apache.commons.logging-1.1.1.jar com.springsource.org.apache.commons.collections-3.2.1.jar ``` 2、創建標準Java工程: (1)選擇“window”—&gt; “Show View” —&gt;“Package Explorer”,使用包結構視圖; ![](img/9f88addce54e8024817d0f4f53f4dfe5__%E9%A1%B9%E7%9B%AE-1.jpg) 圖2-5 包結構視圖 (2)創建標準Java項目,選擇“File”—&gt;“New”—&gt;“Other”;然后在彈出來的對話框中選擇“Java Project”創建標準Java項目; ![](img/7aafae0e483a724ea94d7ad9daf714d9__%E9%A1%B9%E7%9B%AE-2.jpg) 圖2-6 創建Java項目 ![](img/283b4b04411f0e8a7e465b89340eadbe__%E9%A1%B9%E7%9B%AE-3.jpg)? 圖2-7 創建Java項目 ![](img/6b425f63206a21ed2e6bc5e3baf9fec0__%E9%A1%B9%E7%9B%AE-4.jpg) 圖2-8 創建Java項目 (3)配置項目依賴庫文件,右擊項目選擇“Properties”;然后在彈出的對話框中點擊“Add JARS”在彈出的對話框中選擇“lib”目錄下的jar包;然后再點擊“Add Library”,然后在彈出的對話框中選擇“Junit”,選擇“Junit4”; ![](img/266138c7224966ae149dc5232e8df2b7__%E9%A1%B9%E7%9B%AE-6.jpg) 圖2-9 配置項目依賴庫文件 ![](img/f05ff0907debcdd9c30a4b3d293a830a__%E9%A1%B9%E7%9B%AE-7.jpg) 圖2-10 配置項目依賴庫文件 ![](img/309affe60e31cc6d789320f5015407ad__%E9%A1%B9%E7%9B%AE-8.jpg) ![](img/4004020600977b86272047716525f3eb__%E9%A1%B9%E7%9B%AE-9.jpg) 圖2-11 配置項目依賴庫文件 (4)項目目錄結構如下圖所示,其中“src”用于存放java文件;“lib”用于存放jar文件;“resources”用于存放配置文件; ![](img/dd19c5cf9424c6ce640f03a36a13544c__%E9%A1%B9%E7%9B%AE-5.jpg) 圖2-12 項目目錄結構 3、項目搭建好了,讓我們來開發接口,此處我們只需實現打印“Hello World!”,所以我們定義一個“sayHello”接口,代碼如下: 1. package?cn.javass.spring.chapter2.helloworld;?? 2. public?interface?HelloApi?{?? 3. public?void?sayHello();?? 4. }?? 4、接口開發好了,讓我們來通過實現接口來完成打印“Hello World!”功能; 1. package?cn.javass.spring.chapter2.helloworld;?? 2. public?class?HelloImpl?implements?HelloApi?{?? 3. @Override?? 4. public?void?sayHello()?{?? 5. System.out.println("Hello?World!");?? 6. }?? 7. }?? 5、接口和實現都開發好了,那如何使用Spring IoC容器來管理它們呢?這就需要配置文件,讓IoC容器知道要管理哪些對象。讓我們來看下配置文件chapter2/helloworld.xml(放到resources目錄下): 1. &lt;?xml?version="1.0"?encoding="UTF-8"?&gt;?? 2. &lt;beans?? 3. xmlns="http://www.springframework.org/schema/beans"?? 4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?? 5. xmlns:context="http://www.springframework.org/schema/context"?? 6. xsi:schemaLocation="?? 7. http://www.springframework.org/schema/beans????????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd?? 8. http://www.springframework.org/schema/context????????????????http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;?? 9. &lt;!--?id?表示你這個組件的名字,class表示組件類?--&gt;?? 10. &lt;bean?id="hello"?class="cn.javass.spring.chapter2.helloworld.HelloImpl"&gt;&lt;/bean&gt;?? 11. &lt;/beans&gt;?? 6、現在萬一具備,那如何獲取IoC容器并完成我們需要的功能呢?首先應該實例化一個IoC容器,然后從容器中獲取需要的對象,然后調用接口完成我們需要的功能,代碼示例如下: 1. package?cn.javass.spring.chapter2.helloworld;?? 2. import?org.junit.Test;?? 3. import?org.springframework.context.ApplicationContext;?? 4. import?org.springframework.context.support.ClassPathXmlApplicationContext;?? 5. public?class?HelloTest?{?? 6. @Test?? 7. public?void?testHelloWorld()?{?? 8. //1、讀取配置文件實例化一個IoC容器?? 9. ApplicationContext?context?=?new?ClassPathXmlApplicationContext("helloworld.xml");?? 10. //2、從容器中獲取Bean,注意此處完全“面向接口編程,而不是面向實現”?? 11. HelloApi?helloApi?=?context.getBean("hello",?HelloApi.class);?? 12. //3、執行業務邏輯?? 13. helloApi.sayHello();?? 14. }?? 15. }?? 7、自此一個完整的Spring Hello World已完成,是不是很簡單,讓我們深入理解下容器和Bean吧。 ### 2.2.4? 詳解IoC容器 在Spring Ioc容器的代表就是org.springframework.beans包中的BeanFactory接口,BeanFactory接口提供了IoC容器最基本功能;而org.springframework.context包下的ApplicationContext接口擴展了BeanFactory,還提供了與Spring AOP集成、國際化處理、事件傳播及提供不同層次的context實現 (如針對web應用的WebApplicationContext)。簡單說, BeanFactory提供了IoC容器最基本功能,而 ApplicationContext 則增加了更多支持企業級功能支持。ApplicationContext完全繼承BeanFactory,因而BeanFactory所具有的語義也適用于ApplicationContext。 容器實現一覽: ??**XmlBeanFactory:**BeanFactory實現,提供基本的IoC容器功能,可以從classpath或文件系統等獲取資源; (1) ?File file = new File("fileSystemConfig.xml"); Resource resource = new FileSystemResource(file); BeanFactory beanFactory = new XmlBeanFactory(resource); (2) Resource resource = new ClassPathResource("classpath.xml");??????????? ????? BeanFactory beanFactory = new XmlBeanFactory(resource); ??**ClassPathXmlApplicationContext:**ApplicationContext實現,從classpath獲取配置文件; BeanFactory beanFactory =?new ClassPathXmlApplicationContext("classpath.xml"); ??**FileSystemXmlApplicationContext:**ApplicationContext實現,從文件系統獲取配置文件。 BeanFactory beanFactory =?new FileSystemXmlApplicationContext("fileSystemConfig.xml"); 具體代碼請參考cn.javass.spring.chapter2.InstantiatingContainerTest.java。 ApplicationContext接口獲取Bean方法簡介: ? Object getBean(String name) 根據名稱返回一個Bean,客戶端需要自己進行類型轉換; ? T getBean(String name, Class&lt;T&gt; requiredType) 根據名稱和指定的類型返回一個Bean,客戶端無需自己進行類型轉換,如果類型轉換失敗,容器拋出異常; ? T getBean(Class&lt;T&gt; requiredType) 根據指定的類型返回一個Bean,客戶端無需自己進行類型轉換,如果沒有或有多于一個Bean存在容器將拋出異常; ? Map&lt;String, T&gt; getBeansOfType(Class&lt;T&gt; type) 根據指定的類型返回一個鍵值為名字和值為Bean對象的 Map,如果沒有Bean對象存在則返回空的Map。 讓我們來看下IoC容器到底是如何工作。在此我們以xml配置方式來分析一下: **一、準備配置文件**:就像前邊Hello World配置文件一樣,在配置文件中聲明Bean定義也就是為Bean配置元數據。 **二、由IoC容器進行解析元數據:**?IoC容器的Bean Reader讀取并解析配置文件,根據定義生成BeanDefinition配置元數據對象,IoC容器根據BeanDefinition進行實例化、配置及組裝Bean。 **三、實例化IoC容器:**由客戶端實例化容器,獲取需要的Bean。 整個過程是不是很簡單,執行過程如圖2-5,其實IoC容器很容易使用,主要是如何進行Bean定義。下一章我們詳細介紹定義Bean。 ![](img/2e57867020e686671fde7923f891ab69__%E6%9C%AA%E6%A0%87%E9%A2%98-5.jpg) 圖2-5 Spring Ioc容器 ### 2.2.5? 小結 除了測試程序的代碼外,也就是程序入口,所有代碼都沒有出現Spring任何組件,而且所有我們寫的代碼沒有實現框架擁有的接口,因而能非常容易的替換掉Spring,是不是非入侵。 客戶端代碼完全面向接口編程,無需知道實現類,可以通過修改配置文件來更換接口實現,客戶端代碼不需要任何修改。是不是低耦合。 如果在開發初期沒有真正的實現,我們可以模擬一個實現來測試,不耦合代碼,是不是很方便測試。 Bean之間幾乎沒有依賴關系,是不是很容易重用。 [轉自【http://sishuok.com/forum/blogPost/list/2428.html](http://sishuok.com/forum/blogPost/list/2428.html)】
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看