Lifecycle callbacks
接口:
实现Sring 的 InitializingBean 和 DisposableBean 接口。容器会调用对象创建好和销毁前调用afterPropertiesSet()和 destroy()
注解:
@PostConstruct
@PreDestroy
XML:
init-method
destroy-method
<bean id="..." class="..." init-method="init" destroy-method="destroy">
全局:
bean中要定义相关的方法
优先级没有<bean>中定义的初始化和销毁的优先级高
<beans default-init-method="init" default-destroy-method="destroy">
Combining lifecycle mechanisms
执行顺序
初始化方法:
- @PostConstruct 标注的方法
- 接口InitializingBean定义的afterPropertiesSet()方法
- 定制的的init()方法
销毁方法:
- @PreDestroy
- 接口InitializingBean 定义的destroy()方法
- 定制的destroy()方法