关于Spring中几种常见接口执行顺序比较

执行顺序:

静态代码块 > 构造方法 > BeanFactoryAware > ApplicationContextAware > @PostConstruct > InitializingBean > InstantiationAwareBeanPostProcessor > BeanPostProcessor > ApplicationRunner

java 复制代码
/**
 * 执行顺序:
 * 静态代码块 > 构造方法 > BeanFactoryAware > ApplicationContextAware > @PostConstruct > InitializingBean > InstantiationAwareBeanPostProcessor > BeanPostProcessor > ApplicationRunner
 * */
@Component
public class BeanPostProcessorTest implements BeanPostProcessor, InitializingBean, BeanFactoryAware, ApplicationContextAware, InstantiationAwareBeanPostProcessor, ApplicationRunner {
    static Integer count = 1;
    public BeanPostProcessorTest(){
        System.out.println(count++ + "构造函数初始化...");
    }
    static {
        System.out.println(count++ + "静态代码块...");
    }

    @PostConstruct
    public void init(){
        System.out.println(count++ + "PostConstruct...");
    }
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println(count++ + "InstantiationAwareBeanPostProcessor-> postProcessBeforeInstantiation...");

        return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        System.out.println(count++ + "InstantiationAwareBeanPostProcessor-> postProcessAfterInstantiation...");

        return InstantiationAwareBeanPostProcessor.super.postProcessAfterInstantiation(bean, beanName);
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        // 在 Bean 初始化之前添加逻辑
        System.out.println(count++ + "BeanPostProcessor-> postProcessBeforeInitialization...");
        return bean; // 返回原始或修改后的 Bean
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        System.out.println(count++ + "BeanPostProcessor-> postProcessAfterInitialization...");

        // 在 Bean 初始化之后添加逻辑
        return bean; // 返回原始或修改后的 Bean
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println(count++ + "BeanFactoryAware-> setBeanFactory...");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println(count++ + "InitializingBean-> afterPropertiesSet...");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println(count++ + "ApplicationContext-> setApplicationContext...");

    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(count++ + "ApplicationRunner-> run...");
    }
}
相关推荐
ZHE|张恒30 分钟前
Spring Bean 生命周期
java·spring
只想安静的写会代码1 小时前
centos/ubuntu/redhat配置清华源/本地源
linux·运维·服务器
susu10830189111 小时前
ubuntu多块硬盘挂载到同一目录LVM方式
linux·运维·ubuntu
r***F2621 小时前
【漏洞复现】CVE-2019-11043(PHP远程代码执行漏洞)信息安全论文_含漏洞复现完整过程_含Linux环境go语言编译环境安装
linux·golang·php
smaller_maple3 小时前
linux问题记录1
linux·运维·服务器
报错小能手4 小时前
讲讲libevent底层机制
linux·服务器
烤麻辣烫4 小时前
黑马程序员大事件后端概览(表现效果升级版)
java·开发语言·学习·spring·intellij-idea
q***96584 小时前
Spring总结(上)
java·spring·rpc
高级程序源4 小时前
springboot社区医疗中心预约挂号平台app-计算机毕业设计源码16750
java·vue.js·spring boot·mysql·spring·maven·mybatis
i***58675 小时前
springcloud springboot nacos版本对应
spring boot·spring·spring cloud