1.ApplicationContext和Bean的初始化及销毁
在使用上下文时需要做一些准备工作,这些准备工作在prepareBeanFactory()方法中实现。在这个方法中,为容器配置了Classloader,Propertyeditor和BeanPostProcessor等,从而为容器的启动做好了必要的准备。在容器要关闭时,需要完成一系列工作,这些工作在doClose()方法中完成。在这个方法中,先发出容器关闭的信号,然后将Bean逐个关闭,最后关闭容器本身。我们可以通过相关Spring IOC容器的功能定制Bean的初始化和销毁过程。Bean的生命周期:Bean实例的创建,为Bean实例设置属性,调用Bean的初始化方法,应用可以通过容器使用Bean,当容器关闭时,调用Bean的销毁方法。
2.lazy-init属性和预实例化
如果设置了lazy-init属性,那么依赖注入发生在容器初始化的过程中,会对beanDefinitionMap中的所有Bean进行依赖注入,这样在初始化过程结束之后,容器执行getBean()得到的就是已经准备好的Bean,不需要进行依赖注入。
3.FactoryBean的实现
FactoryBean为应用生成需要的对象,FactoryBean是一个能产生或者修饰对象生成工厂的Bean。
4.BeanPostProcessor的实现
BeanPostProcessor的后置处理器是一个监听器,它可以监听容器触发的事件。这个BeanPostProcessor是一个接口类,它有两个接口方法,一个是postProcessBeforeInitialization,在Bean的初始化前提供回调入口;一个是postProcessAfterInitialization,在Bean的初始化后提供回调入口,这两个回调的触发都是和容器管理Bean的声明周期相关的。
调用链如下图所示:
// 初始化Bean实例,调用在容器的回调方法和Bean的初始化方法
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
if(System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
AbstractAutowireCapableBeanFactory.this.invokeAwareMethods(beanName, bean);
return null;
}
}, this.getAccessControlContext());
} else {
this.invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
if(mbd == null || !mbd.isSynthetic()) {
// 调用后置处理器BeanPostProcessors的postProcessBeforeInitialization
wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName);
}
// 调用Bean的初始化方法,这个方法由init-method属性指定,并且如果Bean实现了InitializingBean接口,这个
// Bean的afterPropertiesSet实现也会被调用
try {
this.invokeInitMethods(beanName, wrappedBean, mbd);
} catch (Throwable var6) {
throw new BeanCreationException(mbd != null?mbd.getResourceDescription():null, beanName, "Invocation of init method failed", var6);
}
// 调用后置处理器
if(mbd == null || !mbd.isSynthetic()) {
wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
// 依次回调方法
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException {
Object result = existingBean;
Iterator var4 = this.getBeanPostProcessors().iterator();
do {
if(!var4.hasNext()) {
return result;
}
BeanPostProcessor beanProcessor = (BeanPostProcessor)var4.next();
result = beanProcessor.postProcessBeforeInitialization(result, beanName);
} while(result != null);
return result;
}
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException {
Object result = existingBean;
Iterator var4 = this.getBeanPostProcessors().iterator();
do {
if(!var4.hasNext()) {
return result;
}
BeanPostProcessor beanProcessor = (BeanPostProcessor)var4.next();
result = beanProcessor.postProcessAfterInitialization(result, beanName);
} while(result != null);
return result;
}
4.autowiring的实现
在自动装配中,不需要对Bean属性做显示的依赖关系声明,只需要配置好autowiring属性,IoC容器会根据这个属性的配置,使用反射自动查找属性的类型或者名字,然后基于属性的类型或者名字来自动匹配IoC容器中的Bean,从而自动地完成依赖注入。
// 在populateBean方法中根据name或者type进行autowire
if(mbd.getResolvedAutowireMode() == 1 || mbd.getResolvedAutowireMode() == 2) {
MutablePropertyValues newPvs = new MutablePropertyValues((PropertyValues)pvs);
if(mbd.getResolvedAutowireMode() == 1) {
this.autowireByName(beanName, mbd, bw, newPvs);
}
if(mbd.getResolvedAutowireMode() == 2) {
this.autowireByType(beanName, mbd, bw, newPvs);
}
pvs = newPvs;
}
5.依赖检查
可以在Bean中设置dependency-check属性来指定依赖检查模式,具体是在AbstractAutowireCapableBeanFactory中的createBean方法中完成的,在这个过程中调用checkDependencies进行检查。
6.Bean对IoC容器的感知
如果容器中的Bean要使用容器的状态可以通过实现以下接口BeanNameAware,BeanFactoryAware,ApplicationContextAware,
MessageSourceAware,ApplicationEventPublisherAware,ResourceLoader来实现,在设置Bean的属性之后,调用初始化回调方法之前,Spring会调用aware接口中的setter方法。