文章目錄
-
- 為什麼使用Spring5
- 什麼是IOC容器
- 配置中繼資料
- 執行個體化容器
- XML嵌套
- groovy bean定義DSL
- 使用容器
最近在翻譯Spring Framework Documentation 5.1.8.RELEASE. 覺得還是可以系統的将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也稱為依賴注入(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/beans
https://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/beans
https://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。
除了上面例子中在建立ApplicationContext的時候,加載多個xml檔案,其實我們也可以在xml中通過import來引入其他的xml檔案。
<import resource="daos.xml"/>
resource配置的是要引入的xml的路徑,可以使用相對路徑和絕對路徑。不建議使用相對路徑“…”來引用父目錄中的檔案。這樣做會建立對目前應用程式外部檔案的依賴關系。直接使用絕對路徑又會影響不同部署環境下檔案路徑的位置。是以比較好的方法是在運作時根據JVM系統屬性解析的“$…”占位符。
除了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.PetStoreService
beans {
petStore(PetStoreService, accountDao){
accountDao=accountDao
itemDao=itemDao
}
}
ApplicationContext是進階工廠的接口,它能夠維護不同bean的注冊及其依賴。通過使用方法T getBean(String name, Class requiredType),擷取到bean的執行個體。 ApplicationContext允許您讀取bean定義并通路它們,如下例所示:
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List<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 instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List<String> userList = service.getUsernameList();
使用groovy的時候請注意, daos.groovy 必須寫在services.groovy前面,否則會報bean找不到的錯誤。
還可以使用XmlBeanDefinitionReader和GenericApplicationContext結合的方式:
GenericApplicationContext context = new GenericApplicationContext();
//reader with xml
new XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml");
你也可以使用GroovyBeanDefinitionReader來加載Groovy檔案,如下所示:
GenericApplicationContext context = new GenericApplicationContext();
new GroovyBeanDefinitionReader(context).loadBeanDefinitions("services.groovy", "daos.groovy");
- 區塊鍊從入門到放棄系列教程-涵蓋密碼學,超級賬本,以太坊,Libra,比特币等持續更新
- Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續更新
- Spring 5.X系列教程:滿足你對Spring5的一切想象-持續更新
- java程式員從小工到專家成神之路(2020版)-持續更新中,附詳細文章教程