PostConstruct用法说明

PostConstruct用法说明目的:主要是启动项目并执行特定的初始化(including annotationinjectionandanyinitialization)源码说明:ThePostConstructannotationisusedonamethodthatneedstobeexecutedafterdependencyinjectionisdonetope…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

目的:

主要是启动项目并执行特定的初始化(including annotation injection and any initialization)

源码说明:

The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. 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:

 

  • 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.
  • 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.

  • The method defined on a non-interceptor class MUST HAVE the following signature:

    void <METHOD>()

  • The method on which PostConstruct is applied MAY be public, protected, package private or private.
  • The method MUST NOT be static except for the application client.
  • The method MAY be final.
  • 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.

Since:

Common Annotations 1.0

See Also:

javax.annotation.PreDestroy

javax.annotation.Resource

@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PostConstruct {

}

 

翻译说明:

PostConstruct注释用于在完成依赖注入以执行任何初始化之后需要执行的方法。必须在课程投入使用之前调用此方法。必须在支持依赖注入的所有类上支持此注释。即使类没有请求注入任何资源,也必须调用使用PostConstruct注释的方法。只有一种方法可以使用此注释进行注释。应用PostConstruct注释的方法必须满足以下所有标准:

•除了拦截器之外,方法绝不能有任何参数,在这种情况下,它需要一个由Interceptor规范定义的InvocationContext对象。
•在拦截器类上定义的方法必须具有以下签名之一:
void <METHOD>(InvocationContext)

Object <METHOD>(InvocationContext)抛出异常

注意:PostConstruct拦截器方法不能抛出applicationexceptions,但是如果相同的拦截器方法除了生命周期事件之外插入onbusiness或timeout方法,它也可以被声明为抛出包括java.lang.Exception的已检查异常。如果aPostConstruct拦截器方法返回一个值,则容器会忽略它。

•在非拦截器类上定义的方法必须具有以下签名:
void <METHOD>()

•应用PostConstruct的方法可以是公共的,受保护的,包私有的或私有的。
•除应用程序客户端外,方法不能是静态的。
•方法可能是最终的。
•如果方法抛出未经检查的异常,则除了在EJB可以处理异常并且从中恢复异常的EJB的情况下,该类不能被置于intoservice中。
从以下版本开始:Common Annotations 1.0参见:javax.annotation.PreDestroyjavax.annotation.Resource

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/179142.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java中使用double转BigDecimal的问题

    Java中使用double转BigDecimal的问题先上结论:不要直接用double变量作为构造BigDecimal的参数。线上有这么一段Java代码逻辑:1,接口传来一个JSON串,里面有个数字:57.3。2,解析JSON并把这个数字保存在一个float变量。3,把这个float变量赋值给一个BigDecimal对象,用的是BigDecimal的double参数的构造:newBigDecimal(doubleval)4,把这个BigDecimal保存到MySQL数据库,字段类型是decimal(15,2)。…

    2022年5月24日
    136
  • Luajit 概述「建议收藏」

    Luajit 概述「建议收藏」一、JIT即时编译器JIT:即时编译器。将频繁执行的代码,通过JIT编译器编译成机器码缓存起来,下次再调用时直接执行机器码。相比与原生Lua的逐条执行虚拟机指令效率更高。对于那些只执行一次的代码,则保持于原生Lua一样,逐条执行。JIT带来的效率提升,并不一定能抵消编译效率的下降。当虚拟机执行指令时并不会立刻用JIT进行编译。只有部分指令需要JIT进行编译,JIT将决定那些代码将被编译。延迟编译有…

    2022年10月7日
    1
  • GridView出现不完整–GridView删除滚动条

    GridView出现不完整–GridView删除滚动条

    2021年9月9日
    69
  • MySQL乐观锁(MySQL乐观锁)

    悲观锁与乐观锁的区别悲观锁会把整个对象加锁占为已有后才去做操作,Java中的Synchronized属于悲观锁。悲观锁有一个明显的缺点就是:它不管数据存不存在竞争都加锁,随着并发量增加,且如果锁的时间比较长,其性能开销将会变得很大。乐观锁不获取锁直接做操作,然后通过一定检测手段决定是否更新数据,这种方式下,已经没有所谓的锁概念了,每条线程都直接先去执行操作,计算完成后检测是否与其他线程存在共享数据…

    2022年4月16日
    33
  • conda换成中科大的源,2.更换Conda源「建议收藏」

    conda换成中科大的源,2.更换Conda源「建议收藏」#换源###1.Conda切换为清华源>condaconfig–addchannelshttps://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/>condaconfig–addchannelshttps://mirrors.tuna.tsinghua.edu.cn/anaconda/pkg…

    2022年9月1日
    5
  • Pytest(1)安装与入门[通俗易懂]

    Pytest(1)安装与入门[通俗易懂]pytest介绍pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高。根据pytest的官方网站介绍,它

    2022年7月29日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号