关于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...");
    }
}
相关推荐
Sheffield1 小时前
Docker的跨主机服务与其对应的优缺点
linux·网络协议·docker
Sheffield9 小时前
Alpine是什么,为什么是Docker首选?
linux·docker·容器
Johny_Zhao1 天前
centos7安装部署openclaw
linux·人工智能·信息安全·云计算·yum源·系统运维·openclaw
haibindev1 天前
在 Windows+WSL2 上部署 OpenClaw AI员工的实践与踩坑
linux·wsl2·openclaw
0xDevNull2 天前
Linux切换JDK版本详细教程
linux
进击的丸子2 天前
虹软人脸服务器版SDK(Linux/ARM Pro)多线程调用及性能优化
linux·数据库·后端
茶杯梦轩2 天前
从零起步学习RabbitMQ || 第二章:RabbitMQ 深入理解概念 Producer、Consumer、Exchange、Queue 与企业实战案例
服务器·后端·消息队列
Johny_Zhao4 天前
OpenClaw安装部署教程
linux·人工智能·ai·云计算·系统运维·openclaw