【Spring】Bean的生命周期

在 Spring 容器中,Bean 的生命周期可以分为以下几个阶段:

实例化:创建 Bean 实例。
属性赋值:注入依赖属性。
        BeanPostProcessor 的 postProcessBeforeInitialization 方法:调用所有注册的         BeanPostProcessor 的 postProcessBeforeInitialization 方法。
初始化前:调用 InitializingBean 接口的 afterPropertiesSet 方法。
初始化前:调用 @PostConstruct 注解的方法。
初始化前:调用 init-method 配置的方法。
        BeanPostProcessor 的 postProcessAfterInitialization 方法:调用所有注册的         BeanPostProcessor 的 postProcessAfterInitialization 方法。
使用 Bean:Bean 完成初始化后,可以被其他 Bean 使用。
销毁:调用 DisposableBean 接口的 destroy 方法。
销毁:调用 @PreDestroy 注解的方法。
销毁:调用 destroy-method 配置的方法。


BeanPostProcessor 的调用顺序


1、postProcessBeforeInitialization 方法
在 Bean 的初始化方法(如 afterPropertiesSet、@PostConstruct、init-method)之前调用。
所有注册的 BeanPostProcessor 的 postProcessBeforeInitialization 方法都会按顺序调用。


2、postProcessAfterInitialization 方法
在 Bean 的初始化方法(如 afterPropertiesSet、@PostConstruct、init-method)之后调用。
所有注册的 BeanPostProcessor 的 postProcessAfterInitialization 方法都会按顺序调用。


调用顺序的规则
1、按注册顺序调用:
BeanPostProcessor 的调用顺序是按照它们在 Spring 容器中注册的顺序来执行的。
如果你在配置文件中定义了多个 BeanPostProcessor,它们的调用顺序就是它们在配置文件中出现的顺序。
2、优先级:
如果 BeanPostProcessor 实现了 Ordered 接口,可以指定一个优先级。优先级数值越小,优先级越高。
优先级相同的 BeanPostProcessor 按照注册顺序调用。


示例
假设你有三个 BeanPostProcessor 实现类:CustomBeanPostProcessor1、CustomBeanPostProcessor2 和 CustomBeanPostProcessor3,并且它们都实现了 Ordered 接口。

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;

public class CustomBeanPostProcessor1 implements BeanPostProcessor, Ordered {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("CustomBeanPostProcessor1 before initialization of bean: " + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("CustomBeanPostProcessor1 after initialization of bean: " + beanName);
        return bean;
    }

    @Override
    public int getOrder() {
        return 1;
    }
}

public class CustomBeanPostProcessor2 implements BeanPostProcessor, Ordered {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("CustomBeanPostProcessor2 before initialization of bean: " + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("CustomBeanPostProcessor2 after initialization of bean: " + beanName);
        return bean;
    }

    @Override
    public int getOrder() {
        return 2;
    }
}

public class CustomBeanPostProcessor3 implements BeanPostProcessor, Ordered {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("CustomBeanPostProcessor3 before initialization of bean: " + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("CustomBeanPostProcessor3 after initialization of bean: " + beanName);
        return bean;
    }

    @Override
    public int getOrder() {
        return 3;
    }
}

配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public CustomBeanPostProcessor1 customBeanPostProcessor1() {
        return new CustomBeanPostProcessor1();
    }

    @Bean
    public CustomBeanPostProcessor2 customBeanPostProcessor2() {
        return new CustomBeanPostProcessor2();
    }

    @Bean
    public CustomBeanPostProcessor3 customBeanPostProcessor3() {
        return new CustomBeanPostProcessor3();
    }

    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

运行结果
假设 MyBean 的定义如下:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        // 1. 实例化一个 ApplicationContext 对象
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        // 2. 获取并使用 Bean
        MyBean myBean = context.getBean(MyBean.class);
        myBean.doSomething();
    }
}

输出结果

CustomBeanPostProcessor1 before initialization of bean: myBean CustomBeanPostProcessor2 before initialization of bean: myBean CustomBeanPostProcessor3 before initialization of bean: myBean CustomBeanPostProcessor1 after initialization of bean: myBean CustomBeanPostProcessor2 after initialization of bean: myBean CustomBeanPostProcessor3 after initialization of bean: myBean

MyBean is doing something...

总结
调用顺序:BeanPostProcessor 的 postProcessBeforeInitialization 和 postProcessAfterInitialization 方法按注册顺序调用。
优先级:如果 BeanPostProcessor 实现了 Ordered 接口,可以指定优先级,优先级数值越小,优先级越高。
固定顺序:确保在 Bean 的生命周期中,BeanPostProcessor 的调用顺序是固定的,不会乱序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值