@postConstruct
使用:修饰方法
作用:实例化后, 自动执行该注解 修饰的 方法。
相当于 spring.xml 配置文件中的 <bean init-method=""> 标签的属性 init-method=""
例子
@Component
public class Handler {
@PostConstruct
public void handle(){
System.out.println("PostConstruct");
}
}
@PreDestroy
使用:修饰方法
作用: 销毁前,调用 该注解修饰的方法
相当于 spring.xml 配置文件中的 <bean destroy-method=""> 标签的属性 destroy-method=""
应用场景:一般用在数据库连接池上,销毁前 关闭数据库连接