@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)
上一篇 2022年4月5日 上午8:00
下一篇 2022年4月5日 上午8:35


相关推荐

  • python实现udp_python udp报文解析

    python实现udp_python udp报文解析python的udp攻击ddos攻击upd攻击主要是运用,网络套接字模块和多线程或多进程模块对一个ip不断的发送数据包,代码中都是都是自带库python可直接运行和调用。使用前请注意!!!!!!!!!!!!!!!!!!!!!未成年人请勿使用!!!!!!!!!!!!!!!!!!!请勿非法攻击任何网络!!!!!!!!!!!!!!!!!刑法里涉及计算机犯罪的在第285、286、287条,请自行查阅#coding=’utf-8’importsocket#套接字模块fromthreadingim

    2022年10月3日
    6
  • LeetCode——Longest Substring Without Repeating Characters

    LeetCode——Longest Substring Without Repeating Characters

    2022年1月3日
    47
  • dataguard安装

    dataguard安装1 1 nbsp nbsp nbsp nbsp Dataguard 实施前准备 nbsp nbsp nbsp nbsp Oracle 安装分包括 2 块 安装 DB nbsp 软件和升级 如果是 10g 的话 可以先将 DB 升级到 10 2 0 5 升级的原因是为了避免某些 bug 先安装软件 升级 升级完在用 DBCA nbsp 创建实例 这样比安装 DB nbsp 和创建实例之后升级要简单点 nbsp nbsp nbsp nbsp 1 redo nbsp 文件默认是 50M 改成 100M nbsp nbsp nbsp nbsp 2 每个 redogroup 里改成 2 个 nbsp

    2026年3月19日
    2
  • 这10个Redis使用技巧,提升90%工作效率(建议收藏)

    前言 Redis 在当前的技术社区里是非常热门的。从来自 Antirez 一个小小的个人项目到成为内存数据存储行业的标准,Redis已经走过了很长的一段路。随之而来的一系列最佳实践…

    2021年6月23日
    70
  • php strncmp函数用法,strncmp函数用法详解

    php strncmp函数用法,strncmp函数用法详解strncmp 函数为字符串比较函数 其函数语法为 intstrncmp constchar str1 constchar str2 size tn 功能是把 str1 和 str2 进行比较 strncmp 函数为字符串比较函数 字符串大小的比较是以 ASCII 码表上的顺序来决定 此顺序亦为字符的值 其函数声明为 intstrncmp constchar str1

    2026年3月19日
    2
  • 3D游戏建模真的很累吗?前景怎么样?需要什么基础?[通俗易懂]

    3D游戏建模真的很累吗?前景怎么样?需要什么基础?[通俗易懂]所有行业都是一样的,没有什么容易的,只不过这一行是偏向于技术的,一个有好的建模师月薪10k+是很常见的,这个需要有自己刻苦学习的成果。游戏建模前景在游戏模型行业,你基本不用担心找不到工作,因为游戏模型师人才缺口非常大。举个例子:游戏制作公司的人员配比大多数是这样的:比如100人的三维制作组,可能有60人在做模型贴图,10个人在K动画。只要你保证技能在手,一定是抢手的人才。在几年前游戏建模这个行业不仅仅缺人才,甚至连新手都非常稀缺,那个时候公司愿意招聘实习生,培养他们然后给公司干活,但是工资一定不

    2022年5月12日
    47

发表回复

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

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