关于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...");
    }
}
相关推荐
dsywws4 分钟前
Linux学习笔记之vim入门
linux·笔记·学习
幺零九零零1 小时前
【C++】socket套接字编程
linux·服务器·网络·c++
wyh要好好学习1 小时前
SpringMVC快速上手
java·spring
尢词1 小时前
SpringMVC
java·spring·java-ee·tomcat·maven
wrx繁星点点1 小时前
享元模式:高效管理共享对象的设计模式
java·开发语言·spring·设计模式·maven·intellij-idea·享元模式
free2 小时前
netstat中sendq/recvq用于排查发送端发送数据的问题
服务器
小林熬夜学编程2 小时前
【Linux系统编程】第四十一弹---线程深度解析:从地址空间到多线程实践
linux·c语言·开发语言·c++·算法
力姆泰克2 小时前
看电动缸是如何提高农机的自动化水平
大数据·运维·服务器·数据库·人工智能·自动化·1024程序员节
力姆泰克2 小时前
力姆泰克电动缸助力农业机械装备,提高农机的自动化水平
大数据·服务器·数据库·人工智能·1024程序员节
程思扬3 小时前
为什么Uptime+Kuma本地部署与远程使用是网站监控新选择?
linux·服务器·网络·经验分享·后端·网络协议·1024程序员节