Java中的@PostConstruct注解

 更新时间:2025年04月09日 09:27:37   作者:那些很冒险的梦丶  
本文给大家介绍Java中的@PostConstruct注解的相关知识,通过示例代码给大家补充介绍了Java提供的@PostConstruct注解,Spring是如何实现的,感兴趣的朋友一起看看吧

@PostConstruct基本:

@PostConstruct注解好多人以为是Spring提供的。其实是Java自己的注解。

Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法。被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。

通常我们会是在Spring框架中使用到@PostConstruct注解 该注解的方法在整个Bean初始化中的执行顺序:

Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)

应用:在静态方法中调用依赖注入的Bean中的方法。

package com.example.studySpringBoot.util;
import com.example.studySpringBoot.service.MyMethorClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class MyUtils {
    private static MyUtils          staticInstance = new MyUtils();
    @Autowired
    private MyMethorClassService    myService;
    @PostConstruct
    public void init(){
        staticInstance.myService = myService;
    }
    public static Integer invokeBean(){
        return staticInstance.myService.add(10,20);
    }
}

那么Java提供的@PostConstruct注解,Spring是如何实现的呢?

需要先学习下BeanPostProcessor这个接口:

public interface BeanPostProcessor {
	/**
     * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
	 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
	 * or a custom init-method). The bean will already be populated with property values.
	 * The returned bean instance may be a wrapper around the original.
     * 
     * 任何Bean实例化,并且Bean已经populated(填充属性) 就会回调这个方法
     *
     */
	Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
	/**
	 * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
	 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
	 * or a custom init-method). The bean will already be populated with property values.
	 * The returned bean instance may be a wrapper around the original.
     *
     * 任何Bean实例化,并且Bean已经populated(填充属性) 就会回调这个方法
     *
     */
	Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

那Spring初始化是那里回调这些方法呢?

AbstractApplicationContext.finishBeanFactoryInitialization(...);
    beanFactory.preInstantiateSingletons();
       DefaultListableBeanFactory.getBean(beanName);
          AbstractBeanFactory.doGetBean();
            AbstractAutowireCapableBeanFactory.createBean(....)
                populateBean(beanName, mbd, instanceWrapper);
                initializeBean(...)
                 //调用BeanPostProcessor.postProcessBeforeInitialization()方法
                  applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
                 //调用BeanPostProcessor.postProcessBeforeInitialization()方法
                  applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);

BeanPostProcessor有个实现类CommonAnnotationBeanPostProcessor,就是专门处理@PostConstruct  @PreDestroy注解。

CommonAnnotationBeanPostProcessor的父类InitDestroyAnnotationBeanPostProcessor()
 InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization()
    InitDestroyAnnotationBeanPostProcessor.findLifecycleMetadata()
        // 组装生命周期元数据
        InitDestroyAnnotationBeanPostProcessor.buildLifecycleMetadata()
            // 查找@PostConstruct注释的方法
            InitDestroyAnnotationBeanPostProcessor.initAnnotationType
            // 查找@PreDestroy注释方法
            InitDestroyAnnotationBeanPostProcessor.destroyAnnotationType
 // 反射调用          
 metadata.invokeInitMethods(bean, beanName);    

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

相关文章

  • Shiro整合Springboot和redis,jwt过程中的错误shiroFilterChainDefinition问题

    Shiro整合Springboot和redis,jwt过程中的错误shiroFilterChainDefinition问

    这篇文章主要介绍了Shiro整合Springboot和redis,jwt过程中的错误shiroFilterChainDefinition问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • 从0开始教你开发一个springboot应用

    从0开始教你开发一个springboot应用

    这篇文章主要为大家介绍了从0开始开发一个springboot应用教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • IDEA的TODO的使用方式

    IDEA的TODO的使用方式

    这篇文章主要介绍了IDEA的TODO的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • SpringBoot JavaMailSender发送邮件功能

    SpringBoot JavaMailSender发送邮件功能

    这篇文章主要为大家详细介绍了SpringBoot JavaMailSender发送邮件功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • 使用Spring开启注解AOP的支持放置的位置

    使用Spring开启注解AOP的支持放置的位置

    这篇文章主要介绍了使用Spring开启注解AOP的支持放置的位置,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Java微信公众平台开发(10) 微信自定义菜单的创建实现

    Java微信公众平台开发(10) 微信自定义菜单的创建实现

    这篇文章主要为大家详细介绍了Java微信公众平台开发第十步,微信自定义菜单的创建实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • Idea 配置国内 Maven 源的图文教程

    Idea 配置国内 Maven 源的图文教程

    这篇文章主要介绍了Idea 配置国内 Maven 源的教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-11-11
  • Java内部类知识汇总

    Java内部类知识汇总

    在Java中,在类内部可以定义成员变量与方法,还可以在类的内部定义类.这种在类的内部定义的类称为内部类.而内部类所在的类称为外部类.
    2018-03-03
  • 浅谈Spring Data如何简化数据操作的方法

    浅谈Spring Data如何简化数据操作的方法

    这篇文章主要介绍了看Spring Data如何简化数据操作的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-04-04
  • 浅谈springioc实例化bean的三个方法

    浅谈springioc实例化bean的三个方法

    下面小编就为大家带来一篇浅谈springioc实例化bean的三个方法。小编觉得挺不错的,现在就想给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09

最新评论