1. /**
    2. * The PostConstruct annotation is used on a method that needs to be executed
    3. * after dependency injection is done to perform any initialization. This
    4. * method MUST be invoked before the class is put into service. This
    5. * annotation MUST be supported on all classes that support dependency
    6. * injection. The method annotated with PostConstruct MUST be invoked even
    7. * if the class does not request any resources to be injected. Only one
    8. * method can be annotated with this annotation. The method on which the
    9. * PostConstruct annotation is applied MUST fulfill all of the following
    10. * criteria:
    11. * <p>
    12. * <ul>
    13. * <li>The method MUST NOT have any parameters except in the case of
    14. * interceptors in which case it takes an InvocationContext object as
    15. * defined by the Interceptors specification.</li>
    16. * <li>The method defined on an interceptor class MUST HAVE one of the
    17. * following signatures:
    18. * <p>
    19. * void &#060;METHOD&#062;(InvocationContext)
    20. * <p>
    21. * Object &#060;METHOD&#062;(InvocationContext) throws Exception
    22. * <p>
    23. * <i>Note: A PostConstruct interceptor method must not throw application
    24. * exceptions, but it may be declared to throw checked exceptions including
    25. * the java.lang.Exception if the same interceptor method interposes on
    26. * business or timeout methods in addition to lifecycle events. If a
    27. * PostConstruct interceptor method returns a value, it is ignored by
    28. * the container.</i>
    29. * </li>
    30. * <li>The method defined on a non-interceptor class MUST HAVE the
    31. * following signature:
    32. * <p>
    33. * void &#060;METHOD&#062;()
    34. * </li>
    35. * <li>The method on which PostConstruct is applied MAY be public, protected,
    36. * package private or private.</li>
    37. * <li>The method MUST NOT be static except for the application client.</li>
    38. * <li>The method MAY be final.</li>
    39. * <li>If the method throws an unchecked exception the class MUST NOT be put into
    40. * service except in the case of EJBs where the EJB can handle exceptions and
    41. * even recover from them.</li></ul>
    42. * @since Common Annotations 1.0
    43. * @see javax.annotation.PreDestroy
    44. * @see javax.annotation.Resource
    45. */
    46. /**
    47. * PostConstruct注解用在需要执行的方法上
    48. * 在完成依赖注入以执行任何初始化之后。这
    49. * 方法必须在类投入使用之前调用。这
    50. * 所有支持依赖的类都必须支持注解
    51. * 注射。使用 PostConstruct 注释的方法必须被调用,甚至
    52. * 如果类不请求注入任何资源。只有一个
    53. * 方法可以用这个注解来注解。所采用的方法
    54. * 应用 PostConstruct 注释必须满足以下所有条件
    55. * 标准:
    56. * <p>
    57. * <ul>
    58. * <li>方法不能有任何参数,除非是
    59. * 拦截器,在这种情况下,它需要一个 InvocationContext 对象作为
    60. * 由拦截器规范定义。</li>
    61. * <li>在拦截器类上定义的方法必须具有
    62. * 以下签名:
    63. * <p>
    64. * void <METHOD>(InvocationContext)
    65. * <p>
    66. * 对象 <METHOD>(InvocationContext) 抛出异常
    67. * <p>
    68. * <i>注意:PostConstruct 拦截器方法不能抛出应用程序
    69. * 异常,但可以声明抛出检查异常,包括
    70. * java.lang.Exception 如果相同的拦截器方法插入
    71. * 除了生命周期事件之外的业务或超时方法。如果一个
    72. * PostConstruct 拦截器方法返回一个值,它被忽略
    73. * 容器。</i>
    74. * </li>
    75. * <li>在非拦截器类上定义的方法必须具有
    76. * 以下签名:
    77. * <p>
    78. * 无效 <方法>()
    79. * </li>
    80. * <li>应用 PostConstruct 的方法可以是公共的、受保护的、
    81. * 包私有或私有。</li>
    82. * <li>除了应用程序客户端之外,该方法不得是静态的。</li>
    83. * <li>方法可能是最终的。</li>
    84. * <li>如果方法抛出未经检查的异常,则不得将类放入
    85. * 服务,除了 EJB 可以处理异常的 EJB 和
    86. * 甚至从他们身上恢复过来。</li></ul>
    87. * @since 通用注解 1.0
    88. * @see javax.annotation.PreDestroy
    89. * @see javax.annotation.Resource
    90. */
    91. @Documented
    92. @Retention (RUNTIME)
    93. @Target(METHOD)
    94. public @interface PostConstruct {
    95. }

    当一个class被注解为一个Bean,那么class上被@PostConstruct注解的方法将会在程序启动的时候执行。

    是Java自己的注解

    该注解的方法在整个Bean初始化中的执行顺序:Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)