Java中的@PostConstruct注解的使用

 更新时间:2023年06月25日 08:38:25   作者:wild_man  
本文主要介绍了Java中的@PostConstruct注解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

问题

之前在做后端项目遇到一个奇怪的问题,我装配到Spring容器中的一个Bean在另外一个类中无法被注入,该Bean的类型如下:

@Component
@Data
public class FeishuCrawlerConfig{...}

我使用@Component注解将其装配到Spring容器中,然后在另外一个类中将其自动注入,格式如下:

@Component
public class FeishuTenantTokenEntity {
    @Autowired
    private FeishuCrawlerConfig feishuConfig;
    ...
}

正常来讲feishuConfig这个变量会被自动注入完成初始化,但是后面我在使用这个变量时却抛出了NPE。后来经过跟同事的讨论才发现我是在FeishuTenantTokenEntity的构造方法中调用到了feishuConfig的方法,导致了NPE的产生。构造方法如下:

public FeishuTenantTokenEntity() IOException {
    // refreshTokenValue()方法中使用到feishuConfig的方法
    this.tokenValue = refreshTokenValue();
    this.accessTime = System.currentTimeMillis();
}

产生这个问题的原因是Spring中的Bean在初始化时,会先执行其构造方法,再注入@Autowired注解标注的其他Bean。我们的代码中在构造方法中就使用到了@Autowired注入的feishuConfig对象,这时它还没有初始化完成,自然会抛出NPE。这个问题的解决方法就是使用@PostConstruct注解标注的方法替代FeishuTenantTokenEntity的构造方法,该方法如下:

@PostConstruct
public void init() throws IOException {
    this.tokenValue = refreshTokenValue();
    this.accessTime = System.currentTimeMillis();
}

Spring在初始化Bean时,会在注入@Autowired注解标注的Bean后执行@PostConstruct注解标注的方法。我们在init()方法中使用feishuConfig的方法显然是没问题的,并且它还能替代构造方法的作用。Spring中Bean初始化的执行顺序是构造方法>依赖注入( @Autowired )> @PostConstruct标注的方法

@PostConstruct注解

讲到这里,不得不细说一下@PostConstruct注解。

/**
 * 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 dependenc
 * 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:
 * <p>
 * <ul>
 * <li>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.</li>
 * <li>The method defined on an interceptor class MUST HAVE one of the
 * following signatures:
 * <p>
 * void &#060;METHOD&#062;(InvocationContext)
 * <p>
 * Object &#060;METHOD&#062;(InvocationContext) throws Exception
 * <p>
 * <i>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.</i>
 * </li>
 * <li>The method defined on a non-interceptor class MUST HAVE the
 * following signature:
 * <p>
 * void &#060;METHOD&#062;()
 * </li>
 * <li>The method on which PostConstruct is applied MAY be public, protected,
 * package private or private.</li>
 * <li>The method MUST NOT be static except for the application client.</li>
 * <li>The method MAY be final.</li>
 * <li>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.</li></ul>
 * @since Common Annotations 1.0
 * @see javax.annotation.PreDestroy
 * @see javax.annotation.Resource
 */
@Documented
@Retention (RUNTIME)
@Target(METHOD)
public @interface PostConstruct {
}

这是Java官方对@PostConstruct注解的注释文档,可以看到@PostConstruct注解是用于初始化方法上,该方法在依赖注入完成后执行。Java对被@PostConstruct标注的方法做出了如下限制:

  • 除拦截器方法外,该方法不能有任何参数
  • @PostConstruct标注的拦截器方法不能抛出application异常
  • 该方法可以被public,protected,package private和private修饰
  • 该方法不能为静态的,但是可以被final修饰 简而言之,如果你的类中的构造方法需要使用到依赖注入的变量,你可以用@PostConstruct标注的方法来替代构造方法完成初始化。

Spring如何实现 @PostConstruct注解

我们知道在Spring中,Bean的初始化一般分为实例化,属性赋值,初始化和销毁这四个过程。在实例化阶段的前后,InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation和postProcessAfterInstantiation方法会被调用。在初始化阶段的前后,BeanPostProcessor接口的postProcessBeforeInitialization和postProcessAfterInitialization方法会被调用。开发者也可以继承这些接口拓展功能。如下图所示:

在这四个过程中间,Bean的依赖注入发生在属性赋值这个阶段。Spring会在postProcessBeforeInstantiation方法中也就是依赖注入完成之后调用@PostConstruct注解标注的方法,完成Bean的部分初始化工作。 Spring的具体做法就是在创建Bean时,会将它里面被@PostConstruct注解标注的方法保存到Bean的元数据中,在后面调用postProcessBeforeInstantiation方法时,会利用反射调用Bean的元数据中被 @PostConstruct注解标注的方法,从而完成部分初始化工作。感兴趣的同学可以看看源码。

注意

Java官方已在Java 9中弃用了@PostConstruct注解,并在Java 11中删除了@PostConstruct注解。 实现InitializingBean接口并重写其中的afterPropertiesSet方法也可以实现@PostConstruct注解相同的功能。

/**
 * Interface to be implemented by beans that need to react once all their properties
 * have been set by a {@link BeanFactory}: e.g. to perform custom initialization,
 * or merely to check that all mandatory properties have been set.
 *
 * <p>An alternative to implementing {@code InitializingBean} is specifying a custom
 * init method, for example in an XML bean definition. For a list of all bean
 * lifecycle methods, see the {@link BeanFactory BeanFactory javadocs}.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @see DisposableBean
 * @see org.springframework.beans.factory.config.BeanDefinition#getPropertyValues()
 * @see org.springframework.beans.factory.support.AbstractBeanDefinition#getInitMethodName()
 */
public interface InitializingBean {
   /**
    * Invoked by the containing {@code BeanFactory} after it has set all bean properties
    * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
    * <p>This method allows the bean instance to perform validation of its overall
    * configuration and final initialization when all bean properties have been set.
    * @throws Exception in the event of misconfiguration (such as failure to set an
    * essential property) or if initialization fails for any other reason
    */
   void afterPropertiesSet() throws Exception;
}

到此这篇关于Java中的@PostConstruct注解的使用的文章就介绍到这了,更多相关Java @PostConstruct注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring  AOP的两种使用方法

    Spring  AOP的两种使用方法

    这篇文章主要介绍了Spring AOP的两种使用方法,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • Java使用WeakHashMap实现缓存自动清理

    Java使用WeakHashMap实现缓存自动清理

    在 Java 中,内存管理是一个重要的话题,尤其是在涉及到缓存的实现时,如果缓存项不再被使用,我们希望它们能被自动清理,而不必手动删除,WeakHashMap 就是 Java 提供的一种用于缓存和内存管理的工具,本文将深入探讨如何利用 WeakHashMap 来实现缓存自动清理
    2025-01-01
  • SpringBoot整合Jackson超详细用法(附Jackson工具类)

    SpringBoot整合Jackson超详细用法(附Jackson工具类)

    这篇文章主要介绍了SpringBoot整合Jackson超详细教程,本篇讲的是Jackson的详细用法,Jackson工具类在文章最后,直接复制粘贴即可使用,需要的朋友可以参考下
    2023-03-03
  • Java JVM内存区域详解

    Java JVM内存区域详解

    下面小编就为大家带来一篇基于jvm java内存区域的介绍。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-11-11
  • 详解JAVA中的OPTIONAL

    详解JAVA中的OPTIONAL

    从Java 8引入的一个很有趣的特性是Optional类。Optional类主要解决的问题是臭名昭著的空指针异常(NullPointerException)
    2021-06-06
  • java实战CPU占用过高问题的排查及解决

    java实战CPU占用过高问题的排查及解决

    这篇文章给大家分享了java实战CPU占用过高问题的排查及解决方法,有需要的朋友们可以学习下。
    2018-08-08
  • SpringBoot使用SchedulingConfigurer实现多个定时任务多机器部署问题(推荐)

    SpringBoot使用SchedulingConfigurer实现多个定时任务多机器部署问题(推荐)

    这篇文章主要介绍了SpringBoot使用SchedulingConfigurer实现多个定时任务多机器部署问题,定时任务多机器部署解决方案,方式一拆分,单独拆分出来,单独跑一个应用,方式二是基于aop拦截处理(抢占执行),只要有一个执行,其它都不执行,需要的朋友可以参考下
    2023-01-01
  • Java使用application.property读取文件里面的值

    Java使用application.property读取文件里面的值

    本文通过实例代码给大家介绍了Java使用application.property读取文件里面的值,需要的朋友可以参考下
    2018-10-10
  • 基于Spring Data Jest的Elasticsearch数据统计示例

    基于Spring Data Jest的Elasticsearch数据统计示例

    本篇文章主要介绍了基于Spring Data Jest的Elasticsearch数据统计示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • SpringCloud Alibaba微服务实战之远程Feign请求头丢失问题解决方案

    SpringCloud Alibaba微服务实战之远程Feign请求头丢失问题解决方案

    这篇文章主要介绍了SpringCloud Alibaba微服务实战之远程Feign请求头丢失问题,对SpringCloud Alibaba Feign请求头问题感兴趣的朋友跟随小编一起看看吧
    2024-02-02

最新评论