@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:

  1. 用在void <METHOD>(InvocationContext)方法签名中
  2. 不能为静态
  3. 可以为final

其他相关

  1. 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。
  2. PostConstruct在构造函数之后执行,init()方法之前执行。
  3. 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。
  4. @PostConstruct注解的方法将会在依赖注入完成后被自动调用。
  5. 在方法上加该注解会在项目启动的时候执行该方法,也可以理解为在spring容器初始化的时候执行该方法。

个人理解:

带有@PostContruct注解的方法所在类被加载到容器,如果需要对外提供服务需要进行构造器实例化,在构造器实例化之后自动执行该带有@PostContruct方法完成相应初始化工作才会再向外提供服务.

posted @   Felix_Openmind  阅读(243)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
*{cursor: url(https://files-cdn.cnblogs.com/files/morango/fish-cursor.ico),auto;}
点击右上角即可分享
微信分享提示