1. 实现接口
通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法
public class Demo implements InitializingBean {
// 初始化
public void afterPropertiesSet() throws Exception {
// to do something
}
}
public class Demo implements DisposableBean {
// 销毁
public void destroy() throws Exception {
// to do something
}
}
2. XML属性
通过 元素的init-method/destroy-method属性指定初始化之后/销毁之前调用的操作方法
<bean class="esky.Demo" init-method="load" destroy-method="destroy">
3. 注解
在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用
public class Demo {
@PostConstruct
public void init() {
// to do something
}
@PreDestroy
public void destroy() throws Exception {
// to do something
}
}