@Postconstruct

2025/07/08

此注解位于 javax 包中。既然是位于 javax 包中,我想它肯定是在 spring 出现之前就定义出来的。

注意它的语义:post + construct, 构造函数之后。

spring 框架中针对这种标记是有怎样的处理呢?

源码位置:org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor

优点:和容器不耦合。

另外的替代:

  1. InitializingBean: org.springframework.beans.factory.InitializingBean
  2. @Bean 的 initMethod 属性:org.springframework.context.annotation.Bean#initMethod