@PostConstruct注解相关知识
The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization
主要用于依赖注入之后要完成的任何初始化工作!!!
带有@PostContruct注解的类加载进容器成为组件后需要自动执行的方法
💠This method MUST be invoked before the class is put into service.
💠This annotation MUST be supported on all classes that support dependency injection.
💠The method annotated with PostConstruct MUST be invoked even if the class does not request any resources to be injected.
💠Only one method can be annotated with this annotation.
The method on which the PostConstruct annotation is applied MUST fulfill all of the following criteria:
- 用在
void <METHOD>(InvocationContext)方法签名中 - 不能为静态
- 可以为final
其他相关
- 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。

- PostConstruct在构造函数之后执行,init()方法之前执行。
- 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。
- @PostConstruct注解的方法将会在依赖注入完成后被自动调用。
- 在方法上加该注解会在项目启动的时候执行该方法,也可以理解为在spring容器初始化的时候执行该方法。
个人理解:
带有@PostContruct注解的方法所在类被加载到容器,如果需要对外提供服务需要进行构造器实例化,在构造器实例化之后自动执行该带有@PostContruct方法完成相应初始化工作才会再向外提供服务.
学而不思则罔,思而不学则殆!

浙公网安备 33010602011771号