Spring Bean在BeanFactory生命周期

生命周期过程

  • 如果容器注册InstantiationAwareBeanPostProcessor接口,调用postProcessBeforeInstantiation方法
  • Bean的实例化(调用默认构造器)
  • 如果容器注册InstantiationAwareBeanPostProcessor接口,调用postProcessAfterInstantiation方法
  • 如果容器注册InstantiationAwareBeanPostProcessor接口,调用postProcessPropertyValues方法
  • 根据配置设置属性值
  • 如果Bean实现了BeanNameAware接口,调用BeanNameAware接口的setBeanName方法
  • 如果Bean实现了BeanFactoryAware 接口,调用BeanFactoryAware 接口的setBeanFactory方法
  • 如果容器注册了BeanPostProcessor接口,调用BeanPostProcessor接口的postProcessBeforeInitialization方法
  • 如果Bean实现了InitializingBean接口,调用InitializingBean接口的afterPropertiesSet方法
  • 通过init-method属性配置的初始方法
  • 如果容器注册了BeanPostProcessor接口,调用BeanPostProcessor接口的postProcessAfterInitialization 方法
  • 如果是单例模式,将Bean放入缓存池中;容器销毁时,调用DisposableBean的destroy方法;最后调用destroy-method方法
  • 如果是多例模式,将Bean交给调用者。

测试

package exa.ydoing.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class App {
    public static void main(String[] args) {
        //实例化BeanFactory
        Resource res = new ClassPathResource("ApplicationContext.xml");
        BeanFactory bf = new XmlBeanFactory(res);

        //注册InstantiationAwareBeanPostProcessor后置处理器,因为InstantiationAwareBeanPostProcessor继承了BeanPostProcessor,
        //所以不再单独注册BeanPostProcessor后置处理器
        ((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());

        //获得bean
        MyBean bean = bf.getBean("myBean", MyBean.class);
        bean.printName();

        //销毁所有Bean
        ((XmlBeanFactory)bf).destroySingletons();
    }
}

InstantiationAwareBeanPostProcessor 的实现:

package exa.ydoing.spring;
import java.beans.PropertyDescriptor;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        // TODO Auto-generated method stub
        System.out.println("调用BeanPostProcessor接口的postProcessBeforeInitialization方法");
        return bean;
    }
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // TODO Auto-generated method stub
        System.out.println("调用BeanPostProcessor接口的postProcessAfterInitialization方法");
        return bean;
    }
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        // TODO Auto-generated method stub
        System.out.println("调用InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation方法");
        return null;
    }
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        // TODO Auto-generated method stub
        System.out.println("调用InstantiationAwareBeanPostProcessor接口的postProcessAfterInstantiation方法");
        return true;
    }
    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean,
            String beanName) throws BeansException {
        // TODO Auto-generated method stub
        System.out.println("调用InstantiationAwareBeanPostProcessor接口的postProcessPropertyValues方法");
        return pvs;
    }
}

MyBean的代码:

package exa.ydoing.spring;
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 MyBean implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean{
    private BeanFactory beanFactory;

    private String name;

    private String tag;

    public MyBean(){
        System.out.println("调用构造器实例化Bean");
    }

    public void printName(){
        System.out.println("MyBean name is: " + name);
    }

    public String getTag() {
        return tag;
    }
    public void setTag(String tag) {
        this.tag = tag;
        System.out.println("设置Bean的属性为:" + tag);
    }
    @Override
    public void destroy() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("调用DisposableBean接口的destroy方法");
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("调用InitializingBean接口的afterPropertiesSet方法");
    }
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        // TODO Auto-generated method stub
        this.beanFactory = beanFactory;
        System.out.println("调用BeanFactoryAware接口的setBeanFactory方法");
    }
    @Override
    public void setBeanName(String name) {
        // TODO Auto-generated method stub
        this.name = name;
        System.out.println("调用BeanNameAware接口的setBeanName方法");
    }
}

Bean的配置很简单:

  <bean id="myBean" class="exa.ydoing.spring.MyBean">
      <property name="tag" value="test"/>
  </bean>  

输出:

调用InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation方法
调用构造器实例化Bean
调用InstantiationAwareBeanPostProcessor接口的postProcessAfterInstantiation方法
调用InstantiationAwareBeanPostProcessor接口的postProcessPropertyValues方法
设置Bean的属性为:test
调用BeanNameAware接口的setBeanName方法
调用BeanFactoryAware接口的setBeanFactory方法
调用BeanPostProcessor接口的postProcessBeforeInitialization方法
调用InitializingBean接口的afterPropertiesSet方法
调用BeanPostProcessor接口的postProcessAfterInitialization方法
MyBean name is: myBean
调用DisposableBean接口的destroy方法  
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值