SpringBoot @PostConstruct注解
2020-05-01 本文已影响0人
HAO延WEI
原文链接:https://www.cnblogs.com/YuyuanNo1/p/8184003.html
从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct。这两个注解被用来修饰一个非静态的void()方法.而且这个方法不能有抛出异常声明。
@PostConstruct //方式1
public void someMethod(){
...
}
public @PostConstruct void someMethod(){ //方式2
...
}
Java开发之@PostConstruct和@PreConstruct注解
- 1.@PostConstruct说明
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
- 2.@PreConstruct说明
被@PreConstruct修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreConstruct修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。