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构造函数方法声明无效_如何构造函数

    java构造函数方法声明无效_如何构造函数一、什么是构造函数java构造函数,也叫构造方法,是java中一种特殊的函数。函数名与相同,无返回值。作用:一般用来初始化成员属性和成员方法的,即new对象产生后,就调用了对象了属性和方法。在现实生活中,很多事物一出现,就天生具有某些属性和行为。比如人一出生,就有年龄、身高、体重、就会哭;汽车一出产,就有颜色、有外观、可以运行等。这些,我们就可以将这些天然的属性和行为定义在构造函数中,…

    2025年10月9日
    3
  • android 反编译,反,注射LOG

    android 反编译,反,注射LOG

    2022年1月5日
    58
  • pytorch之DataLoader

    pytorch之DataLoaderpytorch之DataLoader在训练神经网络时,最好是对一个batch的数据进行操作,同时还需要对数据进行shuffle和并行加速等。对此,PyTorch提供了DataLoader帮助实现这些功能。Dataset只负责数据的抽象,一次调用__getitem__只返回一个样本。DataLoader的函数定义如下:DataLoader(dataset,batch_size=1,shu…

    2022年5月6日
    51
  • YUI3的几点说明

    YUI3的几点说明YUI3的几点说明YUI3是一个重量级的前端框架库,它提供了单元测试(YUITest),生成文档(YUIDoc),自动化编译(YUIBuild)等工具,在代码组织方面有统一的微件(widget)框

    2022年7月4日
    24
  • 最受欢迎的网管工具集「建议收藏」

    最受欢迎的网管工具集「建议收藏」最受欢迎的网管工具集★★★日前,美国《NetworkWorld》通过读者调查,选出了最受读者欢迎的网络管理工具,我们也将它们推荐给国内的网管员们,希望能助他们一臂之力,使他们轻松排除网络故障。工具名称:SolarWindsEngineerEdition网址:www.solarwinds.net推荐理由:有读者说:“在不到一小时的时间内,我从网站上下载并安装了SolarWinds的授权版

    2022年10月6日
    5
  • speedup scaleup sizeup

    speedup scaleup sizeup并行算法通常包含三种评价的方法,用来评价算法各方面的优劣。1.speedup评测speedup的方法是,保持数据不变,增加计算机的数目。计算机数目为m时的speedup计算方法如下:speedup(m)=在一台机器上面使用的时间/在m台机器上面使用的时间。该评测指标,如果能够随着m保持一个线性的增长,则表示,多台机器能够很好的缩短所需时间。然而,线性的s

    2025年8月4日
    4

发表回复

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

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