关于@PostConstruct注解


    最近项目中使用了@PostConstruct注解,记录如下。

@PostConstruct是Java自己的注解.
@PostConstruct该注解被用来修饰一个非静态的void()方法.
@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次.

@PostConstruct在构造函数之后执行,init()方法之前执行.

 

 

 

 

 

执行顺序

Constructor >> @Autowired >> @PostConstruct

public Class AAA {
  public AAA() {
     System.out.println("此时b还未被注入: b = " + b);   }
  @Autowired private BBB b; 

  @PostConstruct
private void init() {
    System.out.println(
"@PostConstruct将在依赖注入完成后被自动调用: b = " + b);
  }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM