Spring bean的生命周期

spring bean在容器初始化的时候,开始托管bean

在销毁容器的时候会销毁bean

测试的代码如下:

主main

public class BeanLifeCycle {

    public static void main(String[] args) {
        System.out.println("1.现在开始初始化容器");
        ApplicationContext factory = new ClassPathXmlApplicationContext("bean.xml");

        System.out.println("2.容器初始化成功");
        Person person = factory.getBean("person",Person.class);
        System.out.println(person);

        System.out.println("3.现在开始关闭容器");
        ((ClassPathXmlApplicationContext) factory).registerShutdownHook();
    }
}

BeanFactoryPostProcessor

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    public MyBeanFactoryPostProcessor() {
        super();
        System.out.println("[BeanFactoryPostProcessor] struct 实现类构造器!!");
    }


    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("[BeanFactoryPostProcessor] 调用postProcessBeanFactory方法");
        BeanDefinition bd = configurableListableBeanFactory.getBeanDefinition("person");
        bd.getPropertyValues().addPropertyValue("phone","110");
    }

}

BeanPostProcessor:

public class MyBeanPostProcessor implements BeanPostProcessor {

    public MyBeanPostProcessor(){
        super();
        System.out.println("[BeanPostProcessor] struct 实现类构造器!!");
    }

    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        System.out.println("[BeanPostProcessor] 接口方法 postProcessBeforeInitialization 对属性进行修改");
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        System.out.println("[BeanPostProcessor] 接口方法 postProcessAfterInitialization 对属性进行修改");
        return o;
    }
}

InitialPostProcessAdaptor

public class MyInstantiationAwareBeanPostProcessor extends
        InstantiationAwareBeanPostProcessorAdapter {

    public MyInstantiationAwareBeanPostProcessor(){
        super();
        System.out.println("[InstantiationAwareBeanPostProcessorAdapter] struct 实现类构造器!!");
    }

    // 接口方法,实例化bean之前调用
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        System.out.println("[InstantiationAwareBeanPostProcessorAdapter] 调用 postProcessBeforeInstantiation 方法");
        return super.postProcessBeforeInstantiation(beanClass, beanName);
    }

    // 接口方法,实例化bean之后调用
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("[InstantiationAwareBeanPostProcessorAdapter] 调用 postProcessAfterInitialization 方法");
        return super.postProcessAfterInitialization(bean, beanName);
    }

    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs,
                PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        System.out.println("[InstantiationAwareBeanPostProcessorAdapter] 调用 postProcessPropertyValues方法");
        return super.postProcessPropertyValues(pvs, pds, bean, beanName);
    }
}

Person

public class Person implements BeanFactoryAware,BeanNameAware,
        InitializingBean,DisposableBean{

    private String name;
    private String address;
    private String phone;

    private BeanFactory beanFactory;
    private String beanName;

    public Person(){
        System.out.println("[Person 构造器] 调用Person的构造器实例化");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("[Person 注入属性] 注入属性name"+name);
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        System.out.println("[Person 注入属性] 注入属性address"+address);
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        System.out.println("[Person 注入属性] 注入属性phone"+phone);
        this.phone = phone;
    }

    public String toString(){
        return "Person [address=" + address + ", name=" + name + ", phone=" + phone + "]";
    }
    /**********************************************/
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("[Person BeanFactoryAware接口] 调用BeanFactoryAware.setBeanFactory()");
        this.beanFactory = beanFactory;
    }

    @Override
    public void setBeanName(String beanName) {
        System.out.println("[Person setBeanName] 调用BeanNameAware.setBeanName()");
        this.beanName = beanName;
    }

    // 这是DisposibleBean 接口方法
    @Override
    public void destroy() throws Exception {
        System.out.println("[Person Disposible接口] 调用DispoibleBean.destory方法");
    }

    // 这是InitializingBean 接口方法
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("[Person InitializingBean接口] 调用InitializingBean.afterPropertiesSet()");
    }

    // 通过<bean>的init-method属性指定的初始化方法
    public void myInit(){
        System.out.println("[Person init-method] 调用<bean>的init-method属性指定的初始化方法");
    }

    // 通过<bean>的destory-method属性指定的初始化方法
    public void myDestory(){
        System.out.println("[Person destroy-method] 调用<bean>的destroy-method属性指定的销毁方法");
    }
}

运行的结果是:

1.现在开始初始化容器
[BeanFactoryPostProcessor] struct 实现类构造器!!
[BeanFactoryPostProcessor] 调用postProcessBeanFactory方法
[BeanPostProcessor] struct 实现类构造器!!
[InstantiationAwareBeanPostProcessorAdapter] struct 实现类构造器!!
[InstantiationAwareBeanPostProcessorAdapter] 调用 postProcessBeforeInstantiation 方法
[Person 构造器] 调用Person的构造器实例化
[InstantiationAwareBeanPostProcessorAdapter] 调用 postProcessPropertyValues方法
[Person 注入属性] 注入属性address广州
[Person 注入属性] 注入属性name张三
[Person 注入属性] 注入属性phone110
[Person setBeanName] 调用BeanNameAware.setBeanName()
[Person BeanFactoryAware接口] 调用BeanFactoryAware.setBeanFactory()
[BeanPostProcessor] 接口方法 postProcessBeforeInitialization 对属性进行修改
[Person InitializingBean接口] 调用InitializingBean.afterPropertiesSet()
[Person init-method] 调用<bean>的init-method属性指定的初始化方法
[BeanPostProcessor] 接口方法 postProcessAfterInitialization 对属性进行修改
[InstantiationAwareBeanPostProcessorAdapter] 调用 postProcessAfterInitialization 方法

2.容器初始化成功
Person [address=广州, name=张三, phone=110]

3.现在开始关闭容器
[Person Disposible接口] 调用DispoibleBean.destory方法
[Person destroy-method] 调用<bean>的destroy-method属性指定的销毁方法

参考:

Spring Bean 生命周期 【BeanFactory】

Spring Bean 生命周期(非常详细)【ApplicationContext】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值