initializingbean接口為bean提供了初始化方法的方式,它只包括afterpropertiesset方法,凡是繼承該接口的類,在初始化bean的時(shí)候都會(huì)執(zhí)行該方法。
測試,如下:
1
2
3
4
5
6
7
8
9
10
|
import org.springframework.beans.factory.initializingbean; public class testinitializingbean implements initializingbean{ @override public void afterpropertiesset() throws exception { system.out.println( "ceshi initializingbean" ); } public void testinit(){ system.out.println( "ceshi init-method" ); } } |
配置文件
1
|
<bean id= "testinitializingbean" class = "com.testinitializingbean" ></bean> |
main函數(shù)如下
1
2
3
4
5
|
public class main { public static void main(string[] args){ applicationcontext context = new filesystemxmlapplicationcontext( "/src/main/java/com/beans.xml" ); } } |
測試結(jié)果為:
ceshi initializingbean
這說明在spring初始化bean的時(shí)候,如果bean實(shí)現(xiàn)了initializingbean接口,會(huì)自動(dòng)調(diào)用afterpropertiesset方法。
那么問題來了,在配置bean的時(shí)候使用init-method配置也可以為bean配置初始化方法,那這兩個(gè)哪個(gè)會(huì)先執(zhí)行呢,接下來測試一下,修改配置文件,加上init-method:
1
|
<bean id= "testinitializingbean" class = "com.testinitializingbean" init-method= "testinit" ></bean> |
運(yùn)行程序,得出結(jié)果:
ceshi initializingbean
ceshi init-method
從結(jié)果可以看出,在spring初始化bean的時(shí)候,如果該bean實(shí)現(xiàn)了initializingbean接口,并且同時(shí)在配置文件中指定了init-method,系統(tǒng)則是先調(diào)用afterpropertieset()方法,然后再調(diào)用init-method中指定的方法。
那么這種方式在spring中是怎么實(shí)現(xiàn)的呢,通過查看spring加載bean的源碼類abstractautowiredcapablebeanfactory可以看出其中的奧妙,abstractautowiredcapablebeanfactory類中的invokeinitmethods說的非常清楚,如下:
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
31
32
33
34
35
36
|
protected void invokeinitmethods(string beanname, final object bean, rootbeandefinition mbd) throws throwable { //判斷該bean是否實(shí)現(xiàn)了實(shí)現(xiàn)了initializingbean接口,如果實(shí)現(xiàn)了initializingbean接口,則只掉調(diào)用bean的afterpropertiesset方法 boolean isinitializingbean = (bean instanceof initializingbean); if (isinitializingbean && (mbd == null || !mbd.isexternallymanagedinitmethod( "afterpropertiesset" ))) { if (logger.isdebugenabled()) { logger.debug( "invoking afterpropertiesset() on bean with name '" + beanname + "'" ); } if (system.getsecuritymanager() != null ) { try { accesscontroller.doprivileged( new privilegedexceptionaction<object>() { public object run() throws exception { //直接調(diào)用afterpropertiesset ((initializingbean) bean).afterpropertiesset(); return null ; } },getaccesscontrolcontext()); } catch (privilegedactionexception pae) { throw pae.getexception(); } } else { //直接調(diào)用afterpropertiesset ((initializingbean) bean).afterpropertiesset(); } } if (mbd != null ) { string initmethodname = mbd.getinitmethodname(); //判斷是否指定了init-method方法,如果指定了init-method方法,則再調(diào)用制定的init-method if (initmethodname != null && !(isinitializingbean && "afterpropertiesset" .equals(initmethodname)) && !mbd.isexternallymanagedinitmethod(initmethodname)) { //進(jìn)一步查看該方法的源碼,可以發(fā)現(xiàn)init-method方法中指定的方法是通過反射實(shí)現(xiàn) invokecustominitmethod(beanname, bean, mbd); } } } |
總結(jié):
1、spring為bean提供了兩種初始化bean的方式,實(shí)現(xiàn)initializingbean接口,實(shí)現(xiàn)afterpropertiesset方法,或者在配置文件中通過init-method指定,兩種方式可以同時(shí)使用。
2、實(shí)現(xiàn)initializingbean接口是直接調(diào)用afterpropertiesset方法,比通過反射調(diào)用init-method指定的方法效率要高一點(diǎn),但是init-method方式消除了對(duì)spring的依賴。
3、如果調(diào)用afterpropertiesset方法時(shí)出錯(cuò),則不調(diào)用init-method指定的方法。
spring initializingbean的作用
spring的initializingbean接口有很好的用處,位于spring beans中,它只提供一個(gè)方法afterpropertiesset(),當(dāng)你實(shí)現(xiàn)了該方法后,spring就會(huì)對(duì)你提供框架級(jí)的支持:當(dāng)你通過sring容器生產(chǎn)出實(shí)現(xiàn)了該接口的類的實(shí)例后,它就會(huì)調(diào)用afterpropertiesset方法,通過這個(gè)方法,你可以檢查你的bean是否正確地被初始化了.當(dāng)然,你也可以用init-method方法.這兩種方式可以同時(shí)使用,調(diào)用的順序?yàn)閕nit-method后調(diào)用.
總結(jié)
以上所述是小編給大家介紹的spring中的initializingbean接口的使用,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://www.cnblogs.com/weiqihome/p/8922937.html