javascript
Spring5参考指南:IOC容器
文章目錄
- 為什么使用Spring5
- 什么是IOC容器
- 配置元數據
- 實例化容器
- XML嵌套
- groovy bean定義DSL
- 使用容器
最近在翻譯Spring Framework Documentation 5.1.8.RELEASE. 覺得還是可以系統的將Spring5的知識點系統的再整理一下,于是有了這個Spring5參考指南系列,教程會一直更新,翻譯也會同步進行,敬請期待。
為什么使用Spring5
Spring經過這么多年的發展,已經成為既定的企業級J2EE標準,其最大的優點即是輕量級和其最核心的IOC容器。 Spring最新的版本已經到了5.1.8,Spring5提供了很多新的特性,個人認為最主要的有3點:
-
使用JDK8的新特性
最引人注意的是Spring5使用了JDK8中的lambda表達式,讓代碼變得更加簡潔。 -
響應式編程支持
響應式編程是Spring5中最主要的特性之一,響應式編程提供了另一種編程風格,專注于構建對事件做出響應的應用程序。 Spring5 包含響應流和 Reactor。 -
響應式web框架
Spring5提供了一個最新的響應式Web框架,Spring WebFlux,在編程理念和使用習慣方面和之前的傳統Web框架都有了很大的區別。
當然,我們要擁抱新技術新變化,那么快來學習Spring5吧。
什么是IOC容器
IOC也稱為依賴注入(DI)。它是指對象僅通過構造函數參數、工廠方法的參數或從工廠方法構造或返回對象實例后,通過在其上設置的屬性來定義其依賴項(即與之一起工作的其他對象)的過程。
簡單點說就是通過配置的參數來構造對象,然后通過配置的屬性來實例化其依賴對象。一切都是通過配置來完成的,而不是使用通常的Java new方法來創建對象,也不需要手動去查找或者實例化其依賴對象,一切的一切都是通過Spring IOC容器來實現的。
IOC容器的兩個主要包是:org.spring framework.beans和org.springframework.context包。
如果想使用IOC容器,下面兩個依賴是必須的:
<dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.1.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.8.RELEASE</version></dependency>IOC容器中有兩個非常重要的類:BeanFactory和ApplicationContext。
ApplicationContext是BeanFactory的子類,BeanFactory提供了對Bean的操作接口,ApplicationContext則是表示容器本身,提供了bean操作之外的如AOP接入,事件處理,消息資源接入和應用程序上下文等非常有用的特性。
org.springframework.context.ApplicationContext接口代表著SpringIOC容器,它負責實例化、配置和組裝bean。容器通過讀取配置元數據獲取關于要實例化、配置和組裝的對象的指令。配置元數據以XML、Java注釋或Java代碼來表示。它定義了組成應用程序的對象以及這些對象之間的豐富依賴關系。
如果你是創建一個單體應用,那么Spring提供了兩個非常有用的ApplicationContext實現,ClassPathXMLApplicationContext或FileSystemXMLApplicationContext。
ClassPathXMLApplicationContext是從類路徑去加載要裝載的配置,FileSystemXMLApplicationContext是從文件路徑去裝載。
你只需要在配置中,定義你需要使用的業務對象(POJO),在創建和初始化ApplicationContext之后,您就擁有了一個完全配置且可執行的系統或應用程序.
配置元數據
配置配置,Spring的本質就是通過配置來展示和構建業務對象,通常來說,我們可以使用XML文件來配置,當然現在我們也可以使用Java注解和Java代碼來實現。
Spring配置由容器必須管理的至少一個或多個bean定義組成。基于XML的配置元數據通常使用來表示。Java配置通常在@Configuration中使用@bean注解的方法。
下面是一個基本的基于XML的定義 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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="accountDao"class="com.flydean.daos.JpaAccountDao"><!-- additional collaborators and configuration for this bean go here --></bean><bean id="itemDao" class="com.flydean.daos.JpaItemDao"><!-- additional collaborators and configuration for this bean go here --></bean><!-- more bean definitions for data access objects go here --></beans>其中id是bean的唯一標記,class是bean的類路徑。
實例化容器
Spring容器有很多種實例化方法,比如上面講的單體應用的兩個類:
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");上面已經列出了daos.xml , 這里我們再列出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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><!-- services --><bean id="petStore" class="com.flydean.services.PetStoreService"><property name="accountDao" ref="accountDao"/><property name="itemDao" ref="itemDao"/><!-- additional collaborators and configuration for this bean go here --><constructor-arg ref="accountDao"/></bean><!-- more bean definitions for services go here --></beans>service.xml 里面主要定義了對在定義在daos.xml中的bean的引用。這里的引用方式是通過ref. ref引用了daos.xml里面bean的id。
XML嵌套
除了上面例子中在創建ApplicationContext的時候,加載多個xml文件,其實我們也可以在xml中通過import來引入其他的xml文件。
<import resource="daos.xml"/>resource配置的是要引入的xml的路徑,可以使用相對路徑和絕對路徑。不建議使用相對路徑“…”來引用父目錄中的文件。這樣做會創建對當前應用程序外部文件的依賴關系。直接使用絕對路徑又會影響不同部署環境下文件路徑的位置。所以比較好的方法是在運行時根據JVM系統屬性解析的“$…”占位符。
groovy bean定義DSL
除了xml定義,Spring也可以使用groovy bean來配置。
下面是daos.groovy的定義:
import com.flydean.daos.JpaAccountDao; import com.flydean.daos.JpaItemDao;beans{accountDao(JpaAccountDao){}itemDao(JpaItemDao){} }很簡單,就是定義了2個bean。
下面是services.groovy的定義:
import com.flydean.services.PetStoreServicebeans {petStore(PetStoreService, accountDao){accountDao=accountDaoitemDao=itemDao} }使用容器
ApplicationContext是高級工廠的接口,它能夠維護不同bean的注冊及其依賴。通過使用方法T getBean(String name, Class requiredType),獲取到bean的實例。 ApplicationContext允許您讀取bean定義并訪問它們,如下例所示:
// create and configure beansApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");// retrieve configured instancePetStoreService service = context.getBean("petStore", PetStoreService.class);// use configured instanceList<String> userList = service.getUsernameList();上面講到了groovy bean配置, 下面是怎么使用groovy bean:
// create and configure beans with groovy//daos.groovy 必須寫在services.groovy前面,否則會報bean找不到的錯誤ApplicationContext context = new GenericGroovyApplicationContext("daos.groovy","services.groovy");// retrieve configured instancePetStoreService service = context.getBean("petStore", PetStoreService.class);// use configured instanceList<String> userList = service.getUsernameList();使用groovy的時候請注意, daos.groovy 必須寫在services.groovy前面,否則會報bean找不到的錯誤。
還可以使用XmlBeanDefinitionReader和GenericApplicationContext結合的方式:
GenericApplicationContext context = new GenericApplicationContext();//reader with xmlnew XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml");你也可以使用GroovyBeanDefinitionReader來加載Groovy文件,如下所示:
GenericApplicationContext context = new GenericApplicationContext(); new GroovyBeanDefinitionReader(context).loadBeanDefinitions("services.groovy", "daos.groovy");本教程的源代碼可以參照:spring5-core-workshop 中的container模塊。
更多精彩內容且看:
- 區塊鏈從入門到放棄系列教程-涵蓋密碼學,超級賬本,以太坊,Libra,比特幣等持續更新
- Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續更新
- Spring 5.X系列教程:滿足你對Spring5的一切想象-持續更新
- java程序員從小工到專家成神之路(2020版)-持續更新中,附詳細文章教程
更多作者的博客請查看flydean的博客 : http://www.flydean.com
總結
以上是生活随笔為你收集整理的Spring5参考指南:IOC容器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Libra白皮书解读
- 下一篇: Spring5参考指南:Bean的创建