日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring5参考指南:IOC容器

發(fā)布時間:2024/2/28 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring5参考指南:IOC容器 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 為什么使用Spring5
    • 什么是IOC容器
    • 配置元數(shù)據(jù)
    • 實例化容器
    • XML嵌套
    • groovy bean定義DSL
    • 使用容器

最近在翻譯Spring Framework Documentation 5.1.8.RELEASE. 覺得還是可以系統(tǒng)的將Spring5的知識點系統(tǒng)的再整理一下,于是有了這個Spring5參考指南系列,教程會一直更新,翻譯也會同步進(jìn)行,敬請期待。

為什么使用Spring5

Spring經(jīng)過這么多年的發(fā)展,已經(jīng)成為既定的企業(yè)級J2EE標(biāo)準(zhǔn),其最大的優(yōu)點即是輕量級和其最核心的IOC容器。 Spring最新的版本已經(jīng)到了5.1.8,Spring5提供了很多新的特性,個人認(rèn)為最主要的有3點:

  • 使用JDK8的新特性
    最引人注意的是Spring5使用了JDK8中的lambda表達(dá)式,讓代碼變得更加簡潔。

  • 響應(yīng)式編程支持
    響應(yīng)式編程是Spring5中最主要的特性之一,響應(yīng)式編程提供了另一種編程風(fēng)格,專注于構(gòu)建對事件做出響應(yīng)的應(yīng)用程序。 Spring5 包含響應(yīng)流和 Reactor。

  • 響應(yīng)式web框架
    Spring5提供了一個最新的響應(yīng)式Web框架,Spring WebFlux,在編程理念和使用習(xí)慣方面和之前的傳統(tǒng)Web框架都有了很大的區(qū)別。

當(dāng)然,我們要擁抱新技術(shù)新變化,那么快來學(xué)習(xí)Spring5吧。

什么是IOC容器

IOC也稱為依賴注入(DI)。它是指對象僅通過構(gòu)造函數(shù)參數(shù)、工廠方法的參數(shù)或從工廠方法構(gòu)造或返回對象實例后,通過在其上設(shè)置的屬性來定義其依賴項(即與之一起工作的其他對象)的過程。

簡單點說就是通過配置的參數(shù)來構(gòu)造對象,然后通過配置的屬性來實例化其依賴對象。一切都是通過配置來完成的,而不是使用通常的Java new方法來創(chuàng)建對象,也不需要手動去查找或者實例化其依賴對象,一切的一切都是通過Spring IOC容器來實現(xiàn)的。

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接入,事件處理,消息資源接入和應(yīng)用程序上下文等非常有用的特性。

org.springframework.context.ApplicationContext接口代表著SpringIOC容器,它負(fù)責(zé)實例化、配置和組裝bean。容器通過讀取配置元數(shù)據(jù)獲取關(guān)于要實例化、配置和組裝的對象的指令。配置元數(shù)據(jù)以XML、Java注釋或Java代碼來表示。它定義了組成應(yīng)用程序的對象以及這些對象之間的豐富依賴關(guān)系。

如果你是創(chuàng)建一個單體應(yīng)用,那么Spring提供了兩個非常有用的ApplicationContext實現(xiàn),ClassPathXMLApplicationContext或FileSystemXMLApplicationContext。

ClassPathXMLApplicationContext是從類路徑去加載要裝載的配置,FileSystemXMLApplicationContext是從文件路徑去裝載。

你只需要在配置中,定義你需要使用的業(yè)務(wù)對象(POJO),在創(chuàng)建和初始化ApplicationContext之后,您就擁有了一個完全配置且可執(zhí)行的系統(tǒng)或應(yīng)用程序.

配置元數(shù)據(jù)

配置配置,Spring的本質(zhì)就是通過配置來展示和構(gòu)建業(yè)務(wù)對象,通常來說,我們可以使用XML文件來配置,當(dāng)然現(xiàn)在我們也可以使用Java注解和Java代碼來實現(xiàn)。

Spring配置由容器必須管理的至少一個或多個bean定義組成。基于XML的配置元數(shù)據(jù)通常使用來表示。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的唯一標(biāo)記,class是bean的類路徑。

實例化容器

Spring容器有很多種實例化方法,比如上面講的單體應(yīng)用的兩個類:

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

上面已經(jīng)列出了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嵌套

除了上面例子中在創(chuàng)建ApplicationContext的時候,加載多個xml文件,其實我們也可以在xml中通過import來引入其他的xml文件。

<import resource="daos.xml"/>

resource配置的是要引入的xml的路徑,可以使用相對路徑和絕對路徑。不建議使用相對路徑“…”來引用父目錄中的文件。這樣做會創(chuàng)建對當(dāng)前應(yīng)用程序外部文件的依賴關(guān)系。直接使用絕對路徑又會影響不同部署環(huán)境下文件路徑的位置。所以比較好的方法是在運行時根據(jù)JVM系統(tǒng)屬性解析的“$…”占位符。

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是高級工廠的接口,它能夠維護(hù)不同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結(jié)合的方式:

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模塊。

更多精彩內(nèi)容且看:

  • 區(qū)塊鏈從入門到放棄系列教程-涵蓋密碼學(xué),超級賬本,以太坊,Libra,比特幣等持續(xù)更新
  • Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續(xù)更新
  • Spring 5.X系列教程:滿足你對Spring5的一切想象-持續(xù)更新
  • java程序員從小工到專家成神之路(2020版)-持續(xù)更新中,附詳細(xì)文章教程

更多作者的博客請查看flydean的博客 : http://www.flydean.com

總結(jié)

以上是生活随笔為你收集整理的Spring5参考指南:IOC容器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。