前言
javaconfig 原來是 spring 的一個(gè)子項(xiàng)目,它通過 java 類的方式提供 bean 的定義信息,在 spring4 的版本, javaconfig 已正式成為 spring4 的核心功能 。
本文將詳細(xì)介紹關(guān)于spring中基于java類進(jìn)行配置的相關(guān)內(nèi)容,下面話不多說了,來一起看看詳細(xì)的介紹吧
1 定義 bean
普通的 pojo 只要標(biāo)注了 @configuration 注解,就可以為 spring 容器提供 bean 的定義信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
@configuration public class systemconfig { /** * 定義 bean,并實(shí)例化 * * @return */ @bean public userdao userdao() { return new userdao(); } @bean public deptdao deptdao() { return new deptdao(); } /** * 定義 userservice,并把之前定義的 userdao 與 deptdao 注入進(jìn)來 * * @return */ @bean public userservice userservice() { userservice service = new userservice(); service.setuserdao(userdao()); service.setdeptdao(deptdao()); return service; } } |
這個(gè)類的方法標(biāo)注了 @bean 注解,即為定義 bean, bean 的類型由方法返回值的類型決定,名稱默認(rèn)和方法名同名,也可以通過入?yún)@示指定 bean 名稱,比如 @bean(name=”xxx”)。 @bean 所標(biāo)注的方法體提供了 實(shí)例化 bean 的邏輯 。
以上配置和下面的 xml 是等效的:
1
2
3
4
|
<bean id= "userdao" class = "net.deniro.spring4.conf.userdao" /> <bean id= "deptdao" class = "net.deniro.spring4.conf.deptdao" /> <bean id= "userservice" class = "net.deniro.spring4.conf.userservice" p:userdao-ref= "userdao" p:deptdao-ref= "deptdao" /> |
基于 java 類的配置方式和基于 xml 或者基于注解的配置方式相比——
- java 類的配置方式通過代碼編程的方式,可以更加靈活地實(shí)例化 bean 和裝配 bean 之間的關(guān)系。
- xml 或者基于注解的方式都是通過聲明來定義配置的,所以靈活性上要遜一些,但在配置上更簡單 。
因?yàn)?@configuration 注解類本身已經(jīng)標(biāo)注了 @component,所以這些類可以像那些普通的 bean 一樣被注入到其他的 bean 中。
1
2
3
4
5
6
7
8
9
10
11
12
|
@configuration public class applicationconfig { @autowired private systemconfig systemconfig; @bean public authorityservice authorityservice() { authorityservice service = new authorityservice(); service.setuserdao(systemconfig.userdao()); service.setdeptdao(systemconfig.deptdao()); return service; } } |
spring 會對配置類中所有標(biāo)注了 @bean 的方法使用 aop 增強(qiáng),引入 bean 的生命周期管理邏輯。比如上面的 systemconfig.userdao(),它返回的是對應(yīng) bean 的單例。
在 @bean 中,我們還可以通過標(biāo)注 @scope 注解來控制 bean 的作用范圍:
1
2
3
4
5
|
@scope ( "prototype" ) @bean public deptdao deptdao() { return new deptdao(); } |
這樣每次調(diào)用 deptdao() 方法都會返回一個(gè)新的實(shí)例:
1
2
|
assertnotsame(authorityservice.getdeptdao().hashcode(),authorityservice .getdeptdao().hashcode()); |
注意: 使用基于 java 類進(jìn)行配置,類路徑下必須有 spring aop 與 cglib 庫。
2 啟動(dòng) spring 容器
2.1 只使用 @configuration 類
可以使用 annotationconfigapplicationcontext 類的構(gòu)造函數(shù)傳入標(biāo)注了 @configuration 的 java 類來啟動(dòng) spring 容器 。
1
2
3
4
|
applicationcontext context= new annotationconfigapplicationcontext(systemconfig . class ); userservice userservice= (userservice) context.getbean( "userservice" ); assertnotnull(userservice); |
如果存在多個(gè) @configuration 配置類,那么可以 annotationconfigapplicationcontext 中注冊它們,然后再通過刷新容器應(yīng)用這些配置類:
1
2
3
4
5
6
7
8
9
10
11
|
annotationconfigapplicationcontext context= new annotationconfigapplicationcontext(); //注冊多個(gè)配置類 context.register(systemconfig. class ); context.register(applicationconfig. class ); //刷新容器(應(yīng)用這些配置類) context.refresh(); applicationconfig config=context.getbean(applicationconfig. class ); assertnotnull(config); |
也可以通過 @import 將多個(gè)配置類組裝到一個(gè)配置類中,然后僅需注冊這個(gè)組裝好的配置類 ,即可啟動(dòng)容器:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@configuration @import (systemconfig. class ) public class applicationconfig2 { @autowired private systemconfig systemconfig; @bean public authorityservice authorityservice() { authorityservice service = new authorityservice(); service.setuserdao(systemconfig.userdao()); service.setdeptdao(systemconfig.deptdao()); return service; } } |
單元測試:
1
2
3
4
5
6
7
8
9
|
annotationconfigapplicationcontext context= new annotationconfigapplicationcontext(applicationconfig2. class ); applicationconfig2 config=context.getbean(applicationconfig2. class ); assertnotnull(config); final authorityservice authorityservice = config.authorityservice(); assertnotnull(authorityservice.getdeptdao()); assertnotsame(authorityservice.getdeptdao().hashcode(),authorityservice .getdeptdao().hashcode()); |
2.2 使用 xml 文件引用 @configuration 類的配置
標(biāo)注了 @configuration 的配置類也是一個(gè) bean,所以它也可以被 spring 的 <context:component-scan> 掃描到 。 因此如果希望將配置類組裝到 xml 的配置文件中,并通過 xml 的配置文件啟動(dòng) spring,那么僅需要在 xml 中通過 <context:component-scan> 掃描到相應(yīng)的配置類即可 。
1
2
3
|
<context:component-scan base- package = "net.deniro.spring4.conf" resource-pattern= "applicationconfig2.class" /> |
2.3 在 @configuration 類中引用 xml 文件的配置
在 @configuration 配置類中可以直接通過 @importresource 引入 xml 的配置文件,這樣就可以直接通過 @autowired 引用 xml 配置文件中定義的 bean。
配置文件:
1
2
|
<bean id= "groupdao" class = "net.deniro.spring4.conf.groupdao" /> <bean id= "roledao" class = "net.deniro.spring4.conf.roledao" /> |
@configuration 類:
1
2
3
4
5
6
7
8
9
10
11
12
|
@importresource ( "classpath:beans5-11.xml" ) @configuration public class serviceconfig { @bean @autowired public relationservice relationservice(groupdao groupdao,roledao roledao){ relationservice service= new relationservice(); service.setgroupdao(groupdao); service.setroledao(roledao); return service; } } |
單元測試:
1
2
3
4
5
6
7
8
9
10
|
annotationconfigapplicationcontext context= new annotationconfigapplicationcontext (serviceconfig. class ); serviceconfig config=context.getbean(serviceconfig. class ); assertnotnull(config); relationservice service=config.relationservice((groupdao) context.getbean ( "groupdao" ), (roledao) context .getbean ( "roledao" )); assertnotnull(service.getroledao()); |
只要這些不同形式 bean 的定義信息能夠加載到 spring 容器中,那么 spring 就可以智能的完成 bean 之間的裝配 。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。
原文鏈接:https://www.jianshu.com/p/67fa18a9c8b3