Spring IOC源码剖析
一、容器继承体系
BeanFactory是顶层容器
二、Bean生命周期关键节点
1.数据准备
创建测试bean,实现几个特殊接口
//实现 InitializingBean 接口
public class TestBean implements InitializingBean {
public TestBean() {
System.out.println("------------------TestBean 构造器执行------------------------");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("------------------TestBean afterPropertiesSet执行------------------------");
}
}
//实现 BeanPostProcessor 接口
public class TestBeanPostProcessor implements BeanPostProcessor {
public TestBeanPostProcessor() {
System.out.println("------------------BeanPostProcessor 实现类 构造器------------------------");
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("------------------BeanPostProcessor 实现类 postProcessBeforeInitialization 执行------------------------");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("------------------BeanPostProcessor 实现类 postProcessAfterInitialization 执行------------------------");
return bean;
}
}
//实现 BeanFactoryPostProcessor 接口
public class TestBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public TestBeanFactoryPostProcessor() {
System.out.println("------------------BeanFactoryPostProcessor 实现类 构造器 执行------------------------");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("------------------BeanFactoryPostProcessor 实现类 postProcessBeanFactory 执行------------------------");
}
}
以xml配置形式示例