bean與spring容器的關(guān)系
bean配置信息定義了bean的實現(xiàn)及依賴關(guān)系,spring容器根據(jù)各種形式的bean配置信息在容器內(nèi)部建立bean定義注冊表,然后根據(jù)注冊表加載、實例化bean,并建立bean和bean的依賴關(guān)系,最后將這些準備就緒的bean放到bean緩存池中,以供外層的應用程序進行調(diào)用。
本文將給大家詳細介紹關(guān)于在spring中使用編碼方式動態(tài)配置bean的相關(guān)內(nèi)容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。
1 defaultlistablebeanfactory
defaultlistablebeanfactory 實現(xiàn)了 configurablelistablebeanfactory 接口,可以通過這個類來動態(tài)注入 bean。為了保證注入的 bean 也能被 aop 增強,我們需要實現(xiàn) bean 的工廠后置處理器接口 beanfactorypostprocessor。
需要動態(tài)注入的 bean:
1
2
3
4
5
6
7
8
9
10
|
public class bookservice { bookdao bookdao; public void setbookdao(bookdao bookdao) { this .bookdao = bookdao; } public bookdao getbookdao() { return bookdao; } } |
實現(xiàn) bean 的工廠后置處理器接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@component public class bookservicefactorybean implements beanfactorypostprocessor { public void postprocessbeanfactory(configurablelistablebeanfactory beanfactory) throws beansexception { defaultlistablebeanfactory factory = (defaultlistablebeanfactory) beanfactory; //bean 定義 beandefinitionbuilder builder=beandefinitionbuilder.genericbeandefinition (bookservice. class ); //設(shè)置屬性 builder.addpropertyreference( "bookdao" , "bookdao" ); //注冊 bean 定義 factory.registerbeandefinition( "bookservice1" ,builder.getrawbeandefinition()); //注冊 bean 實例 factory.registersingleton( "bookservice2" , new net.deniro.spring4.dynamic.bookservice()); } } |
這里假設(shè) bookdao 已注入容器(xml 或 注解方式)。
在此,我們既可以注冊 bean 的定義,也可以直接注冊 bean 的實例。
配置:
1
2
|
<context:component-scan base- package = "net.deniro.spring4.dynamic" /> |
單元測試:
1
2
3
4
5
6
|
bookservice bookservice1 = (bookservice) context.getbean( "bookservice1" ); assertnotnull(bookservice1); assertnotnull(bookservice1.getbookdao()); bookservice bookservice2 = (bookservice) context.getbean( "bookservice2" ); assertnotnull(bookservice2); |
2 自定義標簽
為了更好地封裝組件,增強組件的易用性,我們會將組件定義為標簽。
自定義標簽步驟為:
- 采用 xsd 描述自定義標簽的元素屬性。
- 編寫 bean 定義的解析器。
- 注冊自定義標簽的解析器。
- 綁定命名空間解析器。
在 resources 中的 schema 文件夾下創(chuàng)建 bookservice.xsd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?xml version= "1.0" encoding= "utf-8" ?> <xsd:schema xmlns= "http://www.deniro.net/schema/service" xmlns:xsd= "http://www.w3.org/2001/xmlschema" xmlns:beans= "http://www.springframework.org/schema/beans" targetnamespace= "http://www.deniro.net/schema/service" elementformdefault= "qualified" attributeformdefault= "unqualified" > <!-- 導入 beans 命名空間--> <xsd: import namespace= "http://www.springframework.org/schema/beans" /> <!-- 定義 book-service 標簽--> <xsd:element name= "book-service" > <xsd:complextype> <xsd:complexcontent> <xsd:extension base= "beans:identifiedtype" > <!-- 定義 dao 屬性--> <xsd:attribute name= "dao" type= "xsd:string" use= "required" /> </xsd:extension> </xsd:complexcontent> </xsd:complextype> </xsd:element> </xsd:schema> |
接著定義服務標簽解析器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class bookservicedefinitionparser implements beandefinitionparser { public beandefinition parse(element element, parsercontext parsercontext) { //創(chuàng)建 bean 定義 beandefinitionbuilder builder=beandefinitionbuilder.genericbeandefinition (bookservice. class ); //注入自定義的標簽屬性 string dao=element.getattribute( "dao" ); builder.addpropertyreference( "bookdao" ,dao); //注冊 bean 定義 parsercontext.registerbeancomponent( new beancomponentdefinition(builder .getrawbeandefinition(), "bookservice" )); return null ; } } |
然后把剛剛定義好的解析器注冊到命名空間:
1
2
3
4
5
|
public class bookservicenamespacehandler extends namespacehandlersupport { public void init() { registerbeandefinitionparser( "book-service" , new bookservicedefinitionparser()); } } |
接著在 resources 中創(chuàng)建 meta-inf 文件夾,并新建 spring.schemas 與 spring.handlers,這兩個文件分別用于配置自定義標簽的文檔結(jié)構(gòu)文件路徑以及解析自定義命名空間的解析器。
文件路徑
spring.handlers:
http\://www.deniro.net/schema/service=net.deniro.spring4.dynamic.bookservicenamespacehandler
spring.schemas:
http\://www.deniro.net/schema/service.xsd=schema/bookservice.xsd
注意: xsd 文件必須放在 resources 的子孫目錄下。
引用自定義標簽:
1
2
3
4
5
6
7
8
9
10
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xmlns:me= "http://www.deniro.net/schema/service" xsi:schemalocation="http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http: //www.deniro.net/schema/service http://www.deniro.net/schema/service.xsd "> <bean id= "bookdao" class = "net.deniro.spring4.dynamic.bookdao" /> <me:book-service dao= "bookdao" /> </beans> |
這里,我們在頭部引用了自定義標簽,并命名為 “me”,然后就可以使用它咯o(∩_∩)o~
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://www.jianshu.com/p/8acc0968dd9f