spring对我们托管的javaBean都做了什么

spring对我们托管的javaBean都做了什么

生命周期

1.实例化
2.通过setAttr方法注入
3.BeanNameAware.setBeanName()
4.BeanFactoryAware.setBeanFactory()
5.ApplicationContextAware.setApplicationContext()(用ApplicationContext生成以及管理bean的时候)
6.BeanPostProcessor.processBeforeInitialization()
7.Spring配置文件中配置了init-method属性会自动调用其配置的初始化方法。这个方法是没有参数的。
8.DisposableBean.destroy()
9.最后,如果这个Bean的Spring配置中配置了destroy-method属性,会自动调用其配置的销毁方法

public class Player implements BeanNameAware, BeanFactoryAware, BeanPostProcessor, DisposableBean,
        InitializingBean {
    static Logger logger = Logger.getLogger("Player");

    public Player() {
        logger.info("调用构造方法");
    }


    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        logger.info("call BeanFactoryAware.setBeanFactory()");
    }

    @Override
    public void setBeanName(String name) {
        logger.info("call BeanNameAware.setBeanName()");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        logger.info("call InitializingBean.afterPropertiesSet()");
    }

    @Override
    public void destroy() throws Exception {
        logger.info("call DisposableBean.destroy()");
    }

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(AppConfig.class);
        context.refresh();
        Player player = context.getBean(Player.class);
    }
}

@Configuration
public class AppConfig {
    @Bean
    public Player player() {
        return new Player();
    }
}
5月 18, 2020 3:25:07 下午 com.x.x.Player <init>
信息: 调用构造方法
5月 18, 2020 3:25:07 下午 com.x.x.Player setBeanName
信息: call BeanNameAware.setBeanName()
5月 18, 2020 3:25:07 下午 com.x.x.Player setBeanFactory
信息: call BeanFactoryAware.setBeanFactory()
5月 18, 2020 3:25:07 下午 com.x.x.Player afterPropertiesSet
信息: call InitializingBean.afterPropertiesSet()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值