spring容器中bean的生命周期的测试

书上介绍的spring中bean的生命周期的图示如下

在这里插入图片描述

测试用的组件的代码

@Component
public class MyLifeCycleBean implements ApplicationContextAware {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("zhangsan")
    private String name;

    @Value("10")
    private int age;

    public MyLifeCycleBean(){
        logger.info("MyLifeCycleBean()构造方法调用....name = {}", name);
    }

    @PostConstruct
    public void init(){
        logger.info("init()方法调用.....name = {}", name);
    }

    @PreDestroy
    public void destroy(){
        logger.info("destroy()方法调用....name = {}", name);
    }


    /**
     * ApplicationContextAware的回调, 属性注入之后调用
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        logger.info("ApplicationContextAware接口回调.....name = {}", name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

自定义的BeanPostProcessor的代码


@Component
public class MyLifeCycleBeanPostProcessor implements BeanPostProcessor {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * Aware回调调用之后,init-method方法调用之前
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof MyLifeCycleBean){
            logger.info("BeanPostProcessor......postProcessBeforeInitialization()调用了......");
        }
        return bean;
    }


    /**
     * init-method方法调用之后
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof MyLifeCycleBean){
            logger.info("BeanPostProcessor......postProcessAfterInitialization()调用了......");
        }
        return bean;
    }
}

自定义的InstantiationAwareBeanPostProcessor的代码

@Component
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 构造方法之前调用,如果返回值不是null,会造成短路
     */
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if(beanClass == MyLifeCycleBean.class){
            logger.info("InstantiationAwareBeanPostProcessor.......postProcessBeforeInstantiation()调用.....");
            // 下面这段代码如果不注释的话,会造成短路, 短路之后,只有BeanPostProcessor......postProcessAfterInitialization()会调用
            /*MyLifeCycleBean myLifeCycleBean = new MyLifeCycleBean();
            myLifeCycleBean.setName("lisi");
            myLifeCycleBean.setAge(50);
            return myLifeCycleBean;*/
        }
        return null;
    }

    /**
     * 构造方法之后,属性设置之前调用
     */
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if(bean instanceof MyLifeCycleBean){
            MyLifeCycleBean myLifeCycleBean = (MyLifeCycleBean) bean;
            logger.info("InstantiationAwareBeanPostProcessor.......postProcessAfterInstantiation()调用.....name = {}",
                    myLifeCycleBean.getName());
        }
        return true;
    }

    /**
     * 构造方法之后,属性设置之前调用
     */
    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        if(bean instanceof MyLifeCycleBean){
            MyLifeCycleBean myLifeCycleBean = (MyLifeCycleBean) bean;
            logger.info("InstantiationAwareBeanPostProcessor.......postProcessProperties()调用.....name = {}",
                    myLifeCycleBean.getName());
        }
        return pvs;
    }
}

不短路的情况下(MyInstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation()方法返回null),测试的结果如下:

InstantiationAwareBeanPostProcessor.......postProcessBeforeInstantiation()调用.....
MyLifeCycleBean()构造方法调用....name = null
InstantiationAwareBeanPostProcessor.......postProcessAfterInstantiation()调用.....name = null
InstantiationAwareBeanPostProcessor.......postProcessProperties()调用.....name = null
ApplicationContextAware接口回调.....name = zhangsan
BeanPostProcessor......postProcessBeforeInitialization()调用了......
init()方法调用.....name = zhangsan
BeanPostProcessor......postProcessAfterInitialization()调用了......

点击exit按钮之后,输出如下:

destroy()方法调用....name = zhangsan

短路的情况下(MyInstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation()方法返回值不是null), 测试的结果如下:

InstantiationAwareBeanPostProcessor.......postProcessBeforeInstantiation()调用.....
MyLifeCycleBean()构造方法调用....name = null
BeanPostProcessor......postProcessAfterInitialization()调用了......

点击exit按钮之后,没有输出了,也就是说destory()方法也不会调用了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值