前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PostConstruct用法说明

PostConstruct用法说明

作者头像
全栈程序员站长
发布2022-09-27 11:04:50
7470
发布2022-09-27 11:04:50
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

目的:

主要是启动项目并执行特定的初始化(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.cn/179142.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档