@PostConstruct注解介绍

原始类说明

/*
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javax.annotation;

import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;

/**
 * The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. 
 * PostConstruct 注解用于标记在依赖注入完成后需要执行的方法,以执行任何初始化操作。
 * 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. 
 * 被 PostConstruct 注解标记的方法必须被调用,即使类不请求注入任何资源。
 * 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:
 * 被 PostConstruct 注解标记的方法必须满足以下所有条件:
 *
 *
 * 1)The method MUST NOT have any parameters except in the case of interceptors in which case it takes an InvocationContext object as defined by the Interceptors specification.
 * 除了拦截器的情况下,方法不能有任何参数,若是拦截器,则可以接受由拦截器规范定义的 InvocationContext 对象。
 *
 * 2)The method defined on an interceptor class MUST HAVE one of the following signatures:
 *   void <METHOD>(InvocationContext)
 *   Object <METHOD>(InvocationContext) throws Exception
 *   Note: A PostConstruct interceptor method must not throw application exceptions, but it may be declared to throw checked exceptions including the java.lang.Exception if the same interceptor method interposes on business or timeout methods in addition to lifecycle events. If a PostConstruct interceptor method returns a value, it is ignored by the container.
 * 在拦截器类中定义的方法必须具有以下签名之一: 
 *   void (InvocationContext) 
 *   Object (InvocationContext) throws Exception 
 *   注意:PostConstruct 拦截器方法不能抛出应用程序异常,但可以声明抛出受检异常,包括 java.lang.Exception,如果相同的拦截器方法除了生命周期事件外还介入业务或超时方法。如果 PostConstruct 拦截器方法返回一个值,则容器会忽略它。
 *
 * 3)The method defined on a non-interceptor class MUST HAVE the following signature: void <METHOD>()
 * 应用 PostConstruct 注解的非拦截器类中定义的方法必须具有以下签名: void ()
 * 4)The method on which PostConstruct is applied MAY be public, protected, package private or private.
 * 应用 PostConstruct 注解的方法可以是 public、protected、包私有或私有的。
 * 5)The method MUST NOT be static except for the application client.
 * 除了应用程序客户端之外,方法不得为静态的。
 * 6)The method MAY be final.
 * 方法可以是 final 的。
 * 7)If the method throws an unchecked exception the class MUST NOT be put into service except in the case of EJBs where the EJB can handle exceptions and even recover from them.
 * 如果方法抛出未使用,除非是 EJB 的情况,EJB 可以处理异常甚至从中恢复。经检查的异常,则类不能被投入
 * @since Common Annotations 1.0
 * @see javax.annotation.PreDestroy
 * @see javax.annotation.Resource
 */
@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PostConstruct {
}

PostConstruct 注解用于标记在依赖注入完成后需要执行的方法,以执行任何初始化操作。
这个方法必须在类投入使用之前被调用。
这个注解必须被所有支持依赖注入的类所支持。
被 PostConstruct 注解标记的方法必须被调用,即使类不请求注入任何资源。
只能有一个方法被标记为这个注解。
被 PostConstruct 注解标记的方法必须满足以下所有条件:

  • 1)除了拦截器的情况下,方法不能有任何参数,若是拦截器,则可以接受由拦截器规范定义的 InvocationContext 对象。
  • 2)在拦截器类中定义的方法必须具有以下签名之一: void (InvocationContext) Object (InvocationContext) throws Exception 注意:PostConstruct 拦截器方法不能抛出应用程序异常,但可以声明抛出受检异常,包括 java.lang.Exception,如果相同的拦截器方法除了生命周期事件外还介入业务或超时方法。如果 PostConstruct 拦截器方法返回一个值,则容器会忽略它。
  • 3)应用 PostConstruct 注解的非拦截器类中定义的方法必须具有以下签名: void ()
  • 4)应用 PostConstruct 注解的方法可以是 public、protected、包私有或私有的。
  • 5)除了应用程序客户端之外,方法不得为静态的。
  • 6)方法可以是 final 的。
  • 7)如果方法抛出未经检查的异常,则类不能被投入使用,除非是 EJB 的情况,EJB 可以处理异常甚至从中恢复。

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值