<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 4.2 容器概述 org.springframework.context.ApplicationContext 接口代表了 Spring 的 IoC 容器,負責實例化,配置和裝配上述的 bean。容器獲得指示來實例化某對象,配置并裝 配,這都是通過讀取配置元數據實現的。配置元數據在 XML 中,Java 注解或 Java 代碼中來 表示。它允許你表達編寫應用程序的對象,還有對象間豐富的相互依存的關系。 ApplicationContext 接口的一些實現使用 Spring [開箱的支持。在獨立的應用程序 中 , 通 常 是 來 創 建](http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/support/ClassPathXmlApplicationContext.html) [ClassPathXmlApplicationContext](http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/support/ClassPathXmlApplicationContext.html) [或](http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/support/FileSystemXmlApplicationContext.html) [FileSystemXmlApplicationContext](http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/support/FileSystemXmlApplicationContext.html) 的實例。XML 是定義配置元數據的傳統格式,你 可以指示容器使用 Java 的注解或是代碼作為元數據的格式,提供少量的 XML 配置聲明來開 啟對這些額外的元數據格式的支持。 在很多應用場景中,明確的用戶代碼不需要實例化一個或者多個 Spring IoC 容器的實例。 比如,在 Web 應用場景中,在應用程序的 web.xml 中簡單的八(左右)行樣板 J2EE 描述 符 XML 文件通常就夠了(參考 4.14.4 節“對 Web 應用程序方便的應用上下文實例化”)。如 果你正使用 [SpringSource](http://www.springsource.com/produts/sts) [的工具套件](http://www.springsource.com/produts/sts),Eclipse [支持的開發環境或者是](http://www.springsource.org/roo) [Spring ROO](http://www.springsource.org/roo) 這樣的樣板 配置,就可以容易地被創建,點幾下鼠標或按鍵就可以了。 下圖是 Spring 如何工作的高級別視圖。你的應用程序類聯合配置元數據,所以在 ApplicationContext 被創建和實例化后,就得到了一個完全配置的可執行系統或程序。 ![image](https://box.kancloud.cn/2016-01-25_56a58526acc3c.jpg) Spring 的 IoC 容器 ### 4.2.1 配置元數據 正如上圖所示,Spring 的 IoC 容器處理配置元數據的一種形式;這個配置元數據代表了 你作為應用開發人員是如何告訴 Spring 容器在你的應用程序中來實例化,配置并裝配對象 的。 配置元數據傳統上是以直觀的 XML 格式提供的,這是本章的大部分內容使用它來傳達 Spring IoC 容器關鍵概念和功能。 > ![](https://box.kancloud.cn/2016-01-25_56a58526bb420.gif) > 注意 > 基于 XML 的元數據并不是唯一的配置元數據格式。這種配置元數據真正寫入時,Spring 的 IoC 容器本身和這種格式完全脫鉤。 關于 Spring 容器使用元數據格式的信息,可以參考: 基于注解的配置(4.9 節):Spring 2.5 引入了對基于注解元數據的支持。 基于 Java 的配置(4.12 節):從 Spring 3.0 開始,很多由 Spring JavaConfig 項目提供的特 性稱為 Spring Framework 的核心。因此你可以在應用程序外部來定義 bean,使用 Java 代碼而不是 XML 文件。要使用這些新的特性,請參考@Configuration,@Bean, @Import 和@DependsOn 注解 Spring 配置典型的是最少由一個容器必須管理的 bean 定義構成。基于 XML 的配置元數據展示了這些 bean 的配置是用在頂級`<beans/>`元素中的`<bean/>`元素完成的。 這些 bean 的定義對應構成應用程序中真實的對象。比如你定義的服務層的對象,數據訪問對象(Data Access Object,DAO),表示對象比如 Struts 的 Action 實例,基礎設置對象 比如 Hibernate 的 SesstionFactories,JMS 的 Queues 等這些典型的例子。而不用在容 器中定義細粒度的領域模型對象,因為這通常是由 DAO 和業務邏輯負責創建并加載的領域 對象。但是你也可以使用 Spring 和 AspectJ 整合來配置創建在 IoC 容器控制之外的對象。參 考使用在 Spring 中使用 AspectJ 來對領域對象進行依賴注入(8.8.1 節)。 下面的示例展示了基本的基于 XML 的配置元數據的結構: ``` <?xml version="1.0" encoding="UTF-8"?> <beans 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-3.0.xsd"> <bean id="..." class="..."> <!-- 這個bean的合作者和配置在這里編寫 --> </bean> <bean id="..." class="..."> <!-- 這個bean的合作者和配置在這里編寫 --> </bean> <!-- 更多的bean定義在這里編寫 --> </beans> ``` id 屬性是一個字符串,用來標識定義的獨立的 bean。class 屬性定義了 bean 的類型, 需要使用類的完全限定名。id 屬性的值指的就是協作對象。指寫作對象的 XML 在這個示例 中沒有展示;參考依賴(4.4 節)來獲取更多信息。 ### 4.2.2 實例化容器 實例化 Spring 的 IoC 容 器 是 很 簡 單 的 。 定 位 路 徑 或 所 有 路 徑 提 供 給 ApplicationContext 的構造方法,實際上是表示資源的字符串,它就允許容器從各種外 部資源比如本地文件系統,Java 的 CLASSPATH 等來加載配置元數據 ``` ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"}); ``` > ![](https://box.kancloud.cn/2016-01-25_56a58526bb420.gif) > 注意 > 在學習過 Spring 的 IoC 容器之后,你可能想更多了解 Spring 的 Resource 抽象,這在 第 5 章,資源中會描述,它提供了一個從定義 URI 語法的位置讀取輸入流的簡便機制。特別 是,Resource 路徑用來構建應用程序上下文,這會在 5.7 節“應用上下文和資源路徑”中 來描述。 下面的示例展示了服務層代碼(services.xml)的配置文件: ``` <?xml version="1.0" encoding="UTF-8"?> <beans 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-3.0.xsd"> <!-- services --> <bean id="petStore" class="org.springframework.samples.jpetstore.services .PetStoreServiceImpl"> <property name="accountDao" ref="accountDao"/> <property name="itemDao" ref="itemDao"/> <!-- 這個bean的其它合作者和配置在這里編寫--> </bean> <!-- 更多的service bean的定義在這里編寫 --> </beans> ``` 下面的示例展示了數據訪問對象的 daos.xml 文件: ``` <?xml version="1.0" encoding="UTF-8"?> <beans 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-3.0.xsd"> <bean id="accountDao" class="org.springframework.samples.jpetstore.dao.ibat is.SqlMapAccountDao"> <!-- 這個bean的其它合作者和配置在這里編寫 --> </bean> <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.ibat is.SqlMapItemDao"> <!-- 這個bean的其它合作者和配置在這里編寫 --> </bean> <!-- 更多數據訪問對象的定義在這里編寫 --> </beans> ``` 在上面的示例中,服務層代碼由類 PetStoreServiceImpl 和兩個基于 [iBatis](http://ibatis.apache.org/)(iBatis [現已更名為](http://www.mybatis.org/) [MyBatis](http://www.mybatis.org/),譯者注)對象/實體映射框架的數據訪問對象 SqlMapAccountDao 和 SqlMapItemDao 構成。property 的 name 元素指的是 JavaBean 的屬性名,而 ref 元 素指的是其它 bean 定義的名稱。id 和 ref 元素之間的這種聯系表示了兩個協作對象的依賴 關系。要了解更多配置對象依賴的信息,可以參考依賴(4.4 節)。 #### 4.2.2.1 處理基于 XML 的配置元數據 跨越多個 XML 文件中定義 bean 是很有用的。通常每個獨立的 XML 配置文件代表了一 個邏輯層或架構中的一個模塊。 你可以使用 ApplicationContext 的構造方法從所有的 XML 文件片段中來加載 bean。這個 構造方法可以接收多個 Resource 位置,這在之前的部分都已經看到了。另外,可以使用一個或多個`<import/>`元素來從另外的一個或多個文件中加載 bean。比如: ``` <beans> <import resource="services.xml"/> <import resource="resources/messageSource.xml"/> <import resource="/resources/themeSource.xml"/> <bean id="bean1" class="..."/> <bean id="bean2" class="..."/> </beans> ``` 在 這 個示 例 中, 外部 的 bean 通 過 三個 文件 來 加載 ,分 別 是 services.xml , messageSource.xml 和 themeSource.xml。所有的位置路徑都是相對于該引用文件的, 所以 service.xml 必須在和引用文件 相同路 徑中 或 者 是 類 路 徑 下 , 而 messageSource.xml 和 themeSource.xml 必 須 是 在 位 于 引 用 文 件 下 一 級 的 resources 路徑下。正如你看到的,前部的斜杠被忽略了,這是由于路徑都是相對的,最 好就不用斜線。文件的內容會被引入,包括頂級的`<beans/>`元素,根據 Spring 的 Schema 或 DTD,它必須是有效 bean 定義的 XML 文件。 > ![](https://box.kancloud.cn/2016-01-25_56a58526bb420.gif) > 注意 > 在父目錄中使用相對路徑“../”來引用文件,這是可能的,但是不推薦這么做。這么做了會創建一個文件,它是當前應用程序之外的一個依賴。 特別是,這種引用對于“classpath:”的 URL(比如,“classpath:../service.xml”)是不推薦的,運行時的解析過 程會選擇“最近的”類路徑根目錄并且會查看它的父目錄。類路徑配置的修改可能會導 致去選擇一個不同的,不正確的目錄。 你也可以使用資源位置的完全限定名來代替相對路徑:比如,“file: C:/config /services.xml” 或“classpath:/config/services.xml”。這樣的話,要注意你會耦合應用程序的配置到指定 的絕對路徑。對于絕對路徑,一般最好是保持一個間接的使用,比如通過占位符“${...}”, 這會基于運行時環境的 JVM 系統屬性來解決。 ### 4.2.3 使用容器 ApplicationContext 是能維護不同的 bean 和它們依賴注冊的高級工廠接口。使用 `T getBean(Stringname, Class<T> requiredType)`方法你可以獲取 bean 的實例。 ApplicationContext 允許你讀取 bean 并且訪問它們,如下所示: ``` // 創建并配置bean ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"}); // 獲取配置的實例 PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class); // 使用配置的實例 List userList service.getUsernameList(); ``` 使用 getBean()方法來獲取 bean 的實例。ApplicationContext 接口有一些其它方法來獲取 bean,但最好應用程序代碼不使用它們。事實上,應用程序代碼應該沒有 getBean()方法的調用,那么就沒有對 Spring API 的依賴了。比如,Spring 和 Web 框架整 合時,提供了對各種 Web 框架類庫的依賴注入,不如控制器和 JSF 管理的 bean。
                  <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>

                              哎呀哎呀视频在线观看