1、init-method方法,初始化bean的時候執行,可以針對某個具體的bean進行配置。init-method需要在applicationContext.xml配置文檔中bean的定義里頭寫明。例如:<bean id="TestBean" class="nju.software.xkxt.util.TestBean" init-method="init"></bean>
這樣,當TestBean在初始化的時候會執行TestBean中定義的init方法。
2、afterPropertiesSet方法,初始化bean的時候執行,可以針對某個具體的bean進行配置。afterPropertiesSet 必須實現 InitializingBean接口。實現 InitializingBean接口必須實現afterPropertiesSet方法。
3、BeanPostProcessor,針對所有Spring上下文中所有的bean,可以在配置文檔applicationContext.xml中配置一個BeanPostProcessor,然后對所有的bean進行一個初始化之前和之后的代理。BeanPostProcessor接口中有兩個方法: postProcessBeforeInitialization和postProcessAfterInitialization。 postProcessBeforeInitialization方法在bean初始化之前執行, postProcessAfterInitialization方法在bean初始化之后執行。
總之,afterPropertiesSet 和init-method之間的執行順序是afterPropertiesSet 先執行,init-method 后執行。從BeanPostProcessor的作用,可以看出最先執行的是postProcessBeforeInitialization,然后是afterPropertiesSet,然后是init-method,然后是postProcessAfterInitialization。
————————————————
版權聲明:本文為CSDN博主「零點-一條路走到底」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/u013013553/article/details/79038702