Bean初始化之callback
我们可以通过两种方式实现之:
1。实现InitializingBean接口及对应的方法。
void afterPropertiesSet() throws Exception
缺点:代码与Spring产生耦合
2。xml配置中通过init-method
<bean id=“beanID” class=“bean_Class” init-method=“init_method_name”/>
我们可以在init_method_name对应的方法中实现bean的初始化操作。
优点:通过bean自身实现初始callback操作,与Spring解耦。
Bean销毁之callback
同样,我们可以通过类似的两种方式来实现bean销毁时的操作定义
1。实现DisposableBean 接口
void destroy() throws Exception;
2。xml配置文件中指定destroy-method
<bean id=”beanId” class=”bean_class” destroy-method=”cleanup” />
在该bean类中通过cleanup方法实现bean回收操作, 例如连接释放等。