initializingbean接口為bean提供了初始化方法的方式,它只包括afterpropertiesset方法,凡是繼承該接口的類,在初始化bean的時候都會執行該方法。
測試,如下:
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函數如下
1
2
3
4
5
|
public class main { public static void main(string[] args){ applicationcontext context = new filesystemxmlapplicationcontext( "/src/main/java/com/beans.xml" ); } } |
測試結果為:
ceshi initializingbean
這說明在spring初始化bean的時候,如果bean實現了initializingbean接口,會自動調用afterpropertiesset方法。
那么問題來了,在配置bean的時候使用init-method配置也可以為bean配置初始化方法,那這兩個哪個會先執行呢,接下來測試一下,修改配置文件,加上init-method:
1
|
<bean id= "testinitializingbean" class = "com.testinitializingbean" init-method= "testinit" ></bean> |
運行程序,得出結果:
ceshi initializingbean
ceshi init-method
從結果可以看出,在spring初始化bean的時候,如果該bean實現了initializingbean接口,并且同時在配置文件中指定了init-method,系統則是先調用afterpropertieset()方法,然后再調用init-method中指定的方法。
那么這種方式在spring中是怎么實現的呢,通過查看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是否實現了實現了initializingbean接口,如果實現了initializingbean接口,則只掉調用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 { //直接調用afterpropertiesset ((initializingbean) bean).afterpropertiesset(); return null ; } },getaccesscontrolcontext()); } catch (privilegedactionexception pae) { throw pae.getexception(); } } else { //直接調用afterpropertiesset ((initializingbean) bean).afterpropertiesset(); } } if (mbd != null ) { string initmethodname = mbd.getinitmethodname(); //判斷是否指定了init-method方法,如果指定了init-method方法,則再調用制定的init-method if (initmethodname != null && !(isinitializingbean && "afterpropertiesset" .equals(initmethodname)) && !mbd.isexternallymanagedinitmethod(initmethodname)) { //進一步查看該方法的源碼,可以發現init-method方法中指定的方法是通過反射實現 invokecustominitmethod(beanname, bean, mbd); } } } |
總結:
1、spring為bean提供了兩種初始化bean的方式,實現initializingbean接口,實現afterpropertiesset方法,或者在配置文件中通過init-method指定,兩種方式可以同時使用。
2、實現initializingbean接口是直接調用afterpropertiesset方法,比通過反射調用init-method指定的方法效率要高一點,但是init-method方式消除了對spring的依賴。
3、如果調用afterpropertiesset方法時出錯,則不調用init-method指定的方法。
spring initializingbean的作用
spring的initializingbean接口有很好的用處,位于spring beans中,它只提供一個方法afterpropertiesset(),當你實現了該方法后,spring就會對你提供框架級的支持:當你通過sring容器生產出實現了該接口的類的實例后,它就會調用afterpropertiesset方法,通過這個方法,你可以檢查你的bean是否正確地被初始化了.當然,你也可以用init-method方法.這兩種方式可以同時使用,調用的順序為init-method后調用.
總結
以上所述是小編給大家介紹的spring中的initializingbean接口的使用,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/weiqihome/p/8922937.html