PostConstruct源码
package javax.annotation;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PostConstruct {
}
从源码可以看出,PostConstruct注解是java自带的,用于修饰一个非静态的void方法;
使用PostConstruct注解修饰的方法在服务器加载servlet的时候只会被执行一次;
执行顺序:Constructor > @PostConstruct > init方法
使用示例
@Component
public class User1 {
@PostConstruct
public void init(){
System.out.println("postconstruct method");
}
}
spring中PostConstruct作用
PostConstruct在spring中用于bean初始化中资源的加载
加载顺序:Constructor > @Autowired > @PostConstruct