Bean 的生命周期

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37606901/article/details/82532853

Step1:调用无参构造器,创建实例对象。
Step2:调用参数的setter,为属性注入值。
Step3:若 Bean 实现了BeanNameAware 接口,则会执行接口方法 setBeanName(String beanId),使 Bean 类可以获取其在容器中的 id 名称。
Step4:若 Bean 实现了 BeanFactoryAware 接口,则执行接口方法 setBeanFactory(BeanFactory factory),使 Bean 类可以获取到BeanFactory 对象。
Step5 : 若 定 义 并 注 册 了 Bean 后 处 理 器 BeanPostProcessor , 则 执 行 接 口 方 法
postProcessBeforeInitialization()。
Step6:若 Bean 实现了 InitializingBean 接口,则执行接口方法 afterPropertiesSet ()。该方法在 Bean 的所有属性的 set 方法执行完毕后执行,是 Bean 初始化结束的标志,即 Bean 实例化结束。
Step7:若设置了 init-method 方法,则执行。
Step8 : 若 定 义 并 注 册 了 Bean 后 处 理 器 BeanPostProcessor , 则 执 行 接 口 方 法
postProcessAfterInitialization()。
Step9:执行业务方法。
Step10:若Bean 实现了 DisposableBean 接口,则执行接口方法 destroy()。
Step11:若设置了 destroy-method 方法,则执行。

接口:

package com.bjpowernode.ba07;

public interface ISomeService {
    void doSome();
}

接口实现类:

package com.bjpowernode.ba07;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class SomeServiceImpl implements ISomeService, BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
    private String adao;
    private String bdao;

    public void setAdao(String adao) {
        this.adao = adao;
        System.out.println("Step2:执行setter");
    }

    public void setBdao(String bdao) {
        this.bdao = bdao;
        System.out.println("Step2:执行setter");
    }

    public SomeServiceImpl() {
        System.out.println("Step1:执行无参构造器");
    }

    @Override
    public void doSome() {
        System.out.println("Step9:执行doSome()方法");
    }

    public void setUp() {
        System.out.println("Step7:初始化完毕之后");
    }

    public void tearDown() {
        System.out.println("Step11:销毁之前");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("Step3:获取到bean的id = " + name);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("Step4:获取到BeanFactory容器");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Step6:Bean初始化完毕了");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("Step10:实现接口的销毁之前");
    }

}

MyBeanPostProcessor:

package com.bjpowernode.ba07;

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

public class MyBeanPostProcessor implements BeanPostProcessor {

    // bean:表示当前正在进行初始化的Bean对象
    // beanName:表示当前正在进行初始化的Bean对象的id
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("Step5:执行 ----before---()方法");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(final Object bean, String beanName)
            throws BeansException {
        System.out.println("Step8:执行 ----after---()方法");
        return bean;
    }

}

配置文件:

<?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">

    <!-- 注册Service -->
    <bean id="myService" class="com.bjpowernode.ba07.SomeServiceImpl"
            init-method="setUp" destroy-method="tearDown">
        <property name="adao" value="aaa"/>
        <property name="bdao" value="bbb"/>
    </bean>

    <bean class="com.bjpowernode.ba07.MyBeanPostProcessor"/>

</beans>

测试类:

package com.bjpowernode.ba07;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MyTest {

    @Test
    public void test01() {
        // 创建容器对象,加载Spring配置文件
        String resource = "com/bjpowernode/ba07/applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
        ISomeService service = (ISomeService) ac.getBean("myService");
        service.doSome();
        // 对于销毁方法的执行,有两个条件:
        // 1)当前的Bean需要是singleton的
        // 2)要手工关闭容器
        ((ClassPathXmlApplicationContext)ac).close();
    }

}

测试的结果:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Step1:执行无参构造器
Step2:执行setter
Step2:执行setter
Step3:获取到bean的id = myService
Step4:获取到BeanFactory容器
Step5:执行 ----before---()方法
Step6:Bean初始化完毕了
Step7:初始化完毕之后
Step8:执行 ----after---()方法
Step9:执行doSome()方法
Step10:实现接口的销毁之前
Step11:销毁之

猜你喜欢

转载自blog.csdn.net/qq_37606901/article/details/82532853