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()