spring5.0 源码解析 initializeBean
initializeBean
spring 在bean的属性注入之后会执行 initializeBean 方法
该方法主要分为以下几个步骤
- 执行 aware 方法
- 执行 processor.postProcessBeforeInitialization
- 执行 Init-Methods
- 执行 processor.postProcessAfterInitialization
invokeAwareMethods
如果 bean 实现了相应的 Aware 则注入相关的属性
private void invokeAwareMethods(String beanName, Object bean) {
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
ClassLoader bcl = getBeanClassLoader();
if (bcl != null) {
((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
}
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
applyBeanPostProcessorsBeforeInitialization
在初始化之前执行 的 BeanPostProcessors
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
// 获取所有的 BeanPostProcessor 循环执行
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessBeforeInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
spring 实现处理器
-
ApplicationContextAwareProcessor
可以让bean感知到上下文,也是为bean注入容器相关属性的方法
如果bean实现了相关的aware 那么则会在invokeAwareInterfaces方法中设置相关属性public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware || bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware || bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware || bean instanceof ApplicationStartupAware)) { return bean; } AccessControlContext acc = null; if (System.getSecurityManager() != null) { acc = this.applicationContext.getBeanFactory().getAccessControlContext(); } if (acc != null) { AccessController.doPrivileged((PrivilegedAction<Object>) () -> { invokeAwareInterfaces(bean); return null; }, acc); } else { invokeAwareInterfaces(bean); } return bean; }
-
InitDestroyAnnotationBeanPostProcessor
主要完成了 对 @PostConstruct 注解方法的回调;
这个处理器在before init 之前并没有做什么操作
public Object postProcessBeforeInitialization(Object bean, String beanName) {
return bean;
}
- ConfigurationClassPostProcessor
这个 PostProcessor 很复杂 先一放
public Object postProcessBeforeInitialization(Object bean, String beanName) {
if (bean instanceof ImportAware) {
ImportRegistry ir = this.beanFactory.getBean(IMPORT_REGISTRY_BEAN_NAME, ImportRegistry.class);
AnnotationMetadata importingClass = ir.getImportingClassFor(ClassUtils.getUserClass(bean).getName());
if (importingClass != null) {
((ImportAware) bean).setImportMetadata(importingClass);
}
}
return bean;
}
invokeInitMethods
执行 Init-Methods 初始化 implements InitializingBean 所设置的属性
如果bean 实现了 InitializingBean 则调用 InitializingBean
没有实现则调用 init-Method
protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBeanDefinition mbd)
throws Throwable {
boolean isInitializingBean = (bean instanceof InitializingBean);
// InitializingBean中 的 init 回调函数名 afterPropertiesSet 会在这里记录ExternallyManagedInitMethod 中,以便进行生命周期回调
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
if (logger.isTraceEnabled()) {
logger.trace("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
}
if (System.getSecurityManager() != null) {
try {
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
((InitializingBean) bean).afterPropertiesSet();
return null;
}, getAccessControlContext());
}
catch (PrivilegedActionException pae) {
throw pae.getException();
}
}
else {
((InitializingBean) bean).afterPropertiesSet();
}
}
// 如果不存在 在给定bean上调用指定的自定义init方法。
if (mbd != null && bean.getClass() != NullBean.class) {
String initMethodName = mbd.getInitMethodName();
if (StringUtils.hasLength(initMethodName) &&
!(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
!mbd.isExternallyManagedInitMethod(initMethodName)) {
invokeCustomInitMethod(beanName, bean, mbd);
}
}
}
applyBeanPostProcessorsAfterInitialization
在初始化之后执行 的 BeanPostProcessors
eanPostProcessor | before方法 | after方法 |
---|---|---|
ApplicationContextAwareProcessor | 为EnvironmentAware、EmbeddedValueResolverAware、ResourceLoaderAware、ApplicationEventPublisherAware、MessageSourceAware及ApplicationContextAware设置信息 | 没有实现,默认返回bean |
WebApplicationContextServletContextAwareProcessor | 父类ServletContextAwareProcessor,为ServletContextAware设置ServletContext;为ServletConfigAware设置ServletConfig | 直接返回bean |
ImportAwareBeanPostProcessor | ConfigurationClassPostProcessor的内部类,为ImportAware设置ImportMetadata | 直接返回bean |
ConfigurationPropertiesBindingPostProcessor | 使用ConfigurationPropertiesBinder将ProterySource与配置 | @ConfigurationProperties Bean进行绑定 |
AnnotationAwareAspectJAutoProxyCreator | 父类AbstractAutoProxyCreator,直接返回bean | 父类AbstractAutoProxyCreator方法,对bean尝试进程代理直接返回bean |
DataSourceInitializerPostProcessor | 直接返回bean | 当实例化DataSourceh时,启动DataSourceInitializerInvoker的实例化 |
MethodValidationPostProcessor | 父类AbstractAdvisingBeanPostProcessor,直接返回bean | 父类AbstractAdvisingBeanPostProcessor,尝试将当前advisor添加 |
PersistenceExceptionTranslationPostProcessor | 父类AbstractAdvisingBeanPostProcessor,直接返回bean | 父类AbstractAdvisingBeanPostProcessor,尝试将当前advisor添加到当前bean的Advisor链上 |
CommonAnnotationBeanPostProcessor | 父类InitDestroyAnnotationBeanPostProcessor,反射调用初始化方法,如加了注解@PostConstruct的方法 | 直接返回Bean |
AutowiredAnnotationBeanPostProcessor | 父类InstantiationAwareBeanPostProcessorAdapter直接返回bean | 父类InstantiationAwareBeanPostProcessorAdapter直接返回bean |
ApplicationListenerDetector | 返回bean | 如果当前bean是ApplicationListener且是单例的,尝试注册到applicationContext中 |
return wrappedBean;
返回 wrappedBean对象
Initializing与 Disposable 执行顺序
关于在Spring 容器 初始化和销毁 bean 前所做的操作有三种方式定义:
通过@PostConstruct 和 @PreDestroy 方法 实现初始化后和销毁bean之前进行的操作
通过bean实现InitializingBean和 DisposableBean接口
通过 在xml中配置init-method 和 destory-method方法,或者 配置@Bean(initMethod = “initMethod”, destroyMethod = “destroyMethod”) 注解