@PostConstruct注解

@PostConstruct注解好多人以为是Spring提供的。其实是Java自己的注解。Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法。被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。通常我们会是在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);    

 

 

 

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

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

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


相关推荐

  • dell服务器显示器fre,戴尔发布Gaming 24/27游戏显示器新品 支持144/155Hz FreeSync

    dell服务器显示器fre,戴尔发布Gaming 24/27游戏显示器新品 支持144/155Hz FreeSync访问购买页面:具体说来,24英寸机型覆有防眩光涂层(硬度3H),长宽比为16:9、支持1670万色,水平/垂直可视角度为160/170°。此外还有LEDedgelight、ComfortView、戴尔显示管理器、且兼容VESA壁挂安装。接口方面,该机型提供了2×HDMI1.4、DisplayPort、一个USB3.0上联+两个USB3.0下联、耳机…

    2022年5月8日
    39
  • 用JavaScript对GridView进行上移下移,保存排序

    用JavaScript对GridView进行上移下移,保存排序

    2021年7月27日
    54
  • goland激活码最新-激活码分享

    (goland激活码最新)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlTR0LFTT656-eyJsa…

    2022年3月22日
    56
  • Java面试复习体系总结(2021版,持续更新)

    Java面试复习体系总结(2021版)一、Java基础内容Java基础(一):Java集合框架(超详细解析,看完面试不再怕)Java基础(二):迭代器(Iterator)(含使用方法详解)Java基础(三):LinkedList(含使用方法详解)Java基础(四):ArrayList(含使用方法详解)Java基础(五):HashSet(使用方法详解)Java基础(六):HashMap(使用方法详解)Java基础(七):栈Stack(使用方法详解)

    2022年4月9日
    43
  • Linux文本编辑器——vim「建议收藏」

    Linux文本编辑器——vim「建议收藏」Linux文本编辑器edpicoemacsnanogVimvimvivim是文本编辑器不是文字处理工具(officelibofficewps)你的运维生涯99.999999%离不开它rhel(centos)里最小化安装没有vim只有vi,记得装包vim基于perl开发的,所以vim依赖perl环境vim有三种模式分别为:…

    2022年7月26日
    3
  • Rancher首席架构师解读Fleet:它何以管理百万集群?

    Rancher首席架构师解读Fleet:它何以管理百万集群?作者简介DarrenShepherd,RancherLabs联合创始人及首席架构师。在加入Rancher之前,Darren是Citrix的高级首席工程师,他在那里从事CloudStack、OpenStack、Docker的工作,并构建下一代基础设施编排技术。在加入Citrix之前,Darren曾在GoDaddy工作,他设计并领导一个团队实施公有和私有IaaS云。本文转自RancherLabs2020年年初,Rancher开源了海量集群管理项目Fleet,为大量的Kubernetes集群提供集.

    2022年6月6日
    102

发表回复

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

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