spring bean生命周期分析,带实例

本文介绍了Spring Bean的生命周期,包括BeanNameAware、BeanFactoryAware、BeanClassLoaderAware、BeanPostProcessor、InitializingBean、DisposableBean等接口的回调方法,并通过代码实例展示了如何在XML配置中定义初始化和销毁方法。在测试过程中,详细列出了Bean创建和销毁过程中的调用顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:写在前面

本文只涉及流程图和具体实例,不涉及源码级别分析,对源码感兴趣的朋友可以移步这里

2:流程图

在这里插入图片描述

3:实例

3.1:代码

public class MyLifeCycleBean implements BeanNameAware,
        BeanFactoryAware,
        BeanClassLoaderAware,
        BeanPostProcessor,
        InitializingBean,
        DisposableBean {
    private String test;

    // 普通方法
    public void display(){
        System.out.println("普通类实例方法调用...");
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("BeanClassLoaderAware 被调用了。。。");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanFactoryAware 被调用了。。。");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("BeanNameAware 被调用了。。。");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean#destroy被调用了。。。");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean#afterPropertiesSet被调用了。。。");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor#postProcessBeforeInitialization被调用了。。。");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("BeanPostProcessor#postProcessAfterInitialization被调用了。。。");
        return bean;
    }

    public void initMethod(){
        System.out.println("init-method 被调用...");
    }

    public void destroyMethdo(){
        System.out.println("destroy-method 被调用...");
    }
}

3.2:xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="lifeCycle" class="yudaosourcecode.studyhelp.MyLifeCycleBean"
          init-method="initMethod" destroy-method="destroyMethdo">
        <property name="test" value="test"/>
    </bean>
</beans>

3.3:测试

@Test
public void mylifecyclebeantest() {
    ClassPathResource resource
            = new ClassPathResource("mylifecyclebeantest.xml");
    XmlBeanFactory xbf = new XmlBeanFactory(resource);
    // 手动添加后置bean处理器
    xbf.addBeanPostProcessor(new MyLifeCycleBean());
    MyLifeCycleBean lifeCycle
            = xbf.getBean("lifeCycle", MyLifeCycleBean.class);
    lifeCycle.display();
    // 销毁容器
    xbf.destroySingletons();
}

运行:

BeanNameAware 被调用了。。。
BeanClassLoaderAware 被调用了。。。
BeanFactoryAware 被调用了。。。
BeanPostProcessor#postProcessBeforeInitialization被调用了。。。
InitializingBean#afterPropertiesSet被调用了。。。
init-method 被调用...
BeanPostProcessor#postProcessAfterInitialization被调用了。。。
普通类实例方法调用...
DisposableBean#destroy被调用了。。。
destroy-method 被调用...

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值