Initialize the given bean instance, applying factory callbacks as well as init methods and bean post processors. Called from createBean for traditionally defined beans, and from initializeBean for existing bean instances.
初始化bean实例,执行工厂回调以及 init 方法和 bean后处理器(BeanPostProcessor)。对于传统定义的bean,从createBean调用,对于现有的bean实例,从initializeBean调用。
上面是方法的注释,我的理解,这个方法按下面的顺序干了4件事情:
invokeAwareMethods;
postProcessBeforeInitialization ;
invokeInitMethods;
参考:我是spring菜鸟 - 初始化方法
常用的设定方式有以下三种:
1、通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;
2、通过 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;
3、在指定方法上加上@PostConstruct 或 @PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用。
postProcessAfterInitialization;
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareMethods(beanName, bean);
return null;
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);// 处理三种类型的Aware接口
}
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
这个方法处理三种Aware的子接口,1-BeanNameAware;2-BeanClassLoaderAware;3-BeanFactoryAware
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#invokeAwareMethods
private void invokeAwareMethods(final String beanName, final 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) {
// AbstractAutowireCapableBeanFactory是DefaultListableBeanFactory和XmlBeanFactory的父类
// XmlBeanFactory已经@Deprecated
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
额外复习,可忽略:
看到invokeAwareMethods方法,我想到了ApplicationContextAware接口(之前写过一篇:ApplicationContextAware在支付系统中的应用).
我在源码中搜了一下setApplicationContext
(⭐️ 调试技巧:选中方法名,ctrl+shift+G),发现这里调用了setApplicationContext
方法(此时才想起来之前看过这个后置处理器,哎… ).
ApplicationContextAwareProcessor#invokeAwareInterfaces 方法,这个类是BeanPostProcessor。
下面两个方法不用过多解释,就是把BeanFactory中的 beanPostProcessors
的两个方法都执行一遍 。
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsBeforeInitialization
@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessBeforeInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization
@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessAfterInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
org.springframework.context.support.ApplicationContextAwareProcessor#invokeAwareInterfaces
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
AccessControlContext acc = null;
if (System.getSecurityManager() != null &&
(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {
acc = this.applicationContext.getBeanFactory().getAccessControlContext();
}
if (acc != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareInterfaces(bean);
return null;
}, acc);
}
else {
invokeAwareInterfaces(bean);
}
return bean;
}
private void invokeAwareInterfaces(Object bean) {
if (bean instanceof Aware) {
if (bean instanceof EnvironmentAware) {
((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
}
if (bean instanceof EmbeddedValueResolverAware) {
((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
}
if (bean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
}
if (bean instanceof ApplicationEventPublisherAware) {
((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
}
if (bean instanceof MessageSourceAware) {
((MessageSourceAware) bean).setMessageSource(this.applicationContext);
}
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
}
}
}