@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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 怎么用c语言编写单片机流水灯程序,单片机编程入门:单片机流水灯程序[通俗易懂]

    怎么用c语言编写单片机流水灯程序,单片机编程入门:单片机流水灯程序[通俗易懂]今天就来教教大家怎么玩玩51单片机,当然了,首先有一个必要的条件就是你必须要会c语言,目前大学里面有开的关于微机原理的课的,上课的时候,老师还说:“你们要多学学汇编语言,对你们以后学习单片机有用”,而事实上后来才发现c语言才是最重要的。要想玩单片机,就必须要知道最重要的是什么,对于一块51单片机的开发板来说,最重要的就是要认认真真的看它的原理图,原理图才是最重要的。今天先说说最基本的,就是怎么玩流…

    2022年6月10日
    33
  • odrive教程(处理器2O11接口)

    输入接口在开发自定义ODrive控制代码时,建议您的电动机可以自由连续旋转,并且不与行程有限的传动系统连接ODrive可以通过各种端口和协议进行控制。如果您对嵌入式熟悉的话也可以直接在ODrive上运行自定义代码。请参考ODrive固件开发指南。文章目录输入接口引脚说明引脚功能优先级模拟输入霍尔信号反馈引脚native协议Python其它编程语言ASCII协议ArduinoStep/…

    2022年4月14日
    210
  • 《Java多线程编程核心技术》推荐

    《Java多线程编程核心技术》推荐

    2020年11月12日
    202
  • api接口调用

    api接口调用api接口调用CURL是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP、FTP、TELNET等。最爽的是,PHP也支持CURL库。使用PHP的CURL库可以简单和有效

    2022年7月3日
    21
  • gmap绘制路径_cad画可见点

    gmap绘制路径_cad画可见点将在地图上用鼠标左键点击的点绘制出来(两点之间用直线连接),并且能够随地图一起实现等比例缩放和拖拽。GMap主要涉及到几个坐标的转换:经纬度与GPoint的转换:GPoint是gMapControl控件坐标,坐标原点(0,0)位于控件的左上角,这个转换用函数FromLatLngToLocal()完成。GPoint与经纬度的转换:将控件坐标转换成经纬度,用函数FromLocalToL

    2025年7月30日
    4
  • activity的用法总结_pagemanager

    activity的用法总结_pagemanagerActivityManager总结列表1.公共方法(1)getMemoryClass()通过以兆为单位获取当前应用可用的内存大小。(2)getLargeMemoryClass()当开发者在manifest文件中的标签中设置largeHeap属性的值为”true”时,当前应用就可以获取到系统分配的最大堆内存.如果你设置了该值,可以通过ActivityMa

    2025年9月25日
    5

发表回复

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

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