Spring Bean的生命周期

Spring Bean 的生命周期涵盖了 Bean 从创建到销毁的整个过程,主要包括以下几个阶段:

  1. 实例化(Instantiation):

    • Spring 容器会通过反射机制创建 Bean 的实例。这一步仅仅是对象的实例化,并没有进行任何属性的赋值或初始化。
  2. 属性赋值(Populate Properties):

    • 实例化后,Spring 容器会进行依赖注入,为 Bean 的属性设置值。如果 Bean 之间存在依赖关系,Spring 会先创建并初始化这些依赖 Bean,然后再将它们注入到当前 Bean 中。
  3. 初始化前(Pre-initialization):

    • Spring 容器会在 Bean 初始化之前调用任何注册的 BeanPostProcessorpostProcessBeforeInitialization 方法。这一步允许开发者在 Bean 初始化之前对其进行额外的处理。
  4. 初始化(Initialization):

    • 如果 Bean 实现了 InitializingBean 接口,Spring 会调用其 afterPropertiesSet 方法。
    • 同时,如果在 Bean 配置中指定了初始化方法(通过 init-method 属性或 @PostConstruct 注解),Spring 也会调用这些方法。
  5. 初始化后(Post-initialization):

    • 在初始化完成后,Spring 容器会调用任何注册的 BeanPostProcessorpostProcessAfterInitialization 方法。这一步允许开发者在 Bean 初始化完成后对其进行额外的处理。
  6. 使用阶段(Using the Bean):

    • 在完成初始化后,Bean 处于就绪状态,可以被应用程序使用。这段时间内,Bean 可以执行其核心逻辑和功能。
  7. 销毁前(Pre-destroy):

    • 当容器关闭时,Spring 会在销毁 Bean 之前调用任何注册的 DestructionAwareBeanPostProcessorpostProcessBeforeDestruction 方法。这一步允许开发者在 Bean 销毁之前对其进行清理操作。
  8. 销毁(Destruction):

    • 如果 Bean 实现了 DisposableBean 接口,Spring 会调用其 destroy 方法。
    • 同时,如果在 Bean 配置中指定了销毁方法(通过 destroy-method 属性或 @PreDestroy 注解),Spring 也会调用这些方法。

以下是一个详细的代码示例,展示了各个生命周期阶段的处理:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Configuration
public class BeanLifecycleConfig {

    @Bean(initMethod = "customInit", destroyMethod = "customDestroy")
    public MyBean myBean() {
        return new MyBean();
    }

    @Bean
    public BeanPostProcessor customBeanPostProcessor() {
        return new BeanPostProcessor() {
            @Override
            public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof MyBean) {
                    System.out.println("Before Initialization: " + beanName);
                }
                return bean;
            }

            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof MyBean) {
                    System.out.println("After Initialization: " + beanName);
                }
                return bean;
            }
        };
    }

    public static class MyBean implements InitializingBean, DisposableBean {

        public MyBean() {
            System.out.println("Bean Instantiated");
        }

        @PostConstruct
        public void postConstruct() {
            System.out.println("PostConstruct Method");
        }

        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("InitializingBean's afterPropertiesSet Method");
        }

        public void customInit() {
            System.out.println("Custom Init Method");
        }

        @PreDestroy
        public void preDestroy() {
            System.out.println("PreDestroy Method");
        }

        @Override
        public void destroy() throws Exception {
            System.out.println("DisposableBean's destroy Method");
        }

        public void customDestroy() {
            System.out.println("Custom Destroy Method");
        }
    }
}

在这个例子中,MyBean 类展示了 Spring Bean 的完整生命周期:

  • 实例化:通过构造函数创建 Bean 实例。
  • 属性赋值:注入属性(本例中未显式展示)。
  • 初始化前:调用 BeanPostProcessorpostProcessBeforeInitialization 方法。
  • 初始化:调用 @PostConstruct 注解的方法,InitializingBeanafterPropertiesSet 方法和自定义的 init-method
  • 初始化后:调用 BeanPostProcessorpostProcessAfterInitialization 方法。
  • 使用阶段:Bean 处于就绪状态,可以被使用。
  • 销毁前:调用 DestructionAwareBeanPostProcessorpostProcessBeforeDestruction 方法(本例中未显式展示)。
  • 销毁:调用 @PreDestroy 注解的方法,DisposableBeandestroy 方法和自定义的 destroy-method
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翱翔-蓝天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值