本篇文章我们分析一下Spring源码中refresh()--prepareBeanFactory以下两个方法。
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
首先我们可以看到在prepareBeanFactory这个方法里面有多个addBeanPostProcessor方法的调用。以下是源码,可以发现是存的一个BeanPostProcessor接口实现类的数组;说明ApplicationContextAwareProcessor这个类和ApplicationListenerDetector类是BeanPostProcessor的实现类。
public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {
/** BeanPostProcessors to apply in createBean */
private final List beanPostProcessors = new ArrayList<>();
}
我们接着去看一下BeanPostProcessor接口是什么东西。以下是源码,其实我们从名称里面就大概知道是什么意思。就是对象初始化途中,必须会经过这两个方法。那么这两个方法是做什么用的。我们接着往下看
public interface BeanPostProcessor {
/**
* Apply this BeanPostProcessor to the given new bean instance before any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* The default implementation returns the given {@code bean} as-is.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
* if {@code null}, no subsequent BeanPostProcessors will be invoked
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
*/
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
/**
* Apply this BeanPostProcessor to the given new bean instance after any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
*
In case of a FactoryBean, this callback will be invoked for both the FactoryBean
* instance and the objects created by the FactoryBean (as of Spring 2.0). The
* post-processor can decide whether to apply to either the FactoryBean or created
* objects or both through corresponding {@code bean instanceof FactoryBean} checks.
*
This callback will also be invoked after a short-circuiting triggered by a
* {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
* in contrast to all other BeanPostProcessor callbacks.
*
The default implementation returns the given {@code bean} as-is.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
* if {@code null}, no subsequent BeanPostProcessors will be invoked
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
* @see org.springframework.beans.factory.FactoryBean
*/
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
我们先看一个BeanPostProcessor接口的实现类 ApplicationContextAwareProcessor 重点看这个类的实现方法,看源码我们知道,这个invokeAwareInterfaces方法做的事情, bean instanceof Aware 表示bean对象是不是Aware接口的实现类的实例对象。如果是 就会进行一系列的初始化操作。给属性赋值。
@Override
@Nullable
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
那么我们看一下 Aware接口到底是什么。其实我们可以看一个的例子。可以看到BeanNameAware 这个方法提供了一个setBeanName的方法。可以看到注释,其实就是设置实现了这个接口的类的实例的name。依次类推,其它所以实现了Aware的接口都是设置自己相关属性。所以我们可以看到上面源码中一系列的if条件判断比如if (bean instanceof EnvironmentAware) if (bean instanceof EmbeddedValueResolverAware) 等。其实都是设置自己相关属性。也就是初始化属性。
public interface BeanNameAware extends Aware {
/**
* Set the name of the bean in the bean factory that created this bean.
* Invoked after population of normal bean properties but before an
* init callback such as {@link InitializingBean#afterPropertiesSet()}
* or a custom init-method.
* @param name the name of the bean in the factory.
* Note that this name is the actual bean name used in the factory, which may
* differ from the originally specified name: in particular for inner bean
* names, the actual bean name might have been made unique through appending
* "#..." suffixes. Use the {@link BeanFactoryUtils#originalBeanName(String)}
* method to extract the original bean name (without suffix), if desired.
*/
void setBeanName(String name);
}
接下来我们分析一下beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));这个中的ApplicationListenerDetector。首先我们可以知道,是往容器(beanFactory)中又加了一个BeanPostProcessor的。到时候spring是会进行遍历调用BeanPostProcessor的postProcessAfterInitialization这个方法的。所以我们看postProcessAfterInitialization中做了上面。可以看到会首先判断bean是不是ApplicationListener这个实现类的实例。如果是就会把它添加进当前容器的ApplicationListener属性中,其实这里判断的是该bean是不是实现了ApplicationListener这个接口。如果实现了这个接口,我们就把它 当成是一个监听者。其实这里就是在进行判断bean初始化过程中进行判断,是不是监听者对象,如果是监听者对象,我们就把这个对象添加到监听者对象数组中,以备后用。如果当前bean不是监听者,当前postProcessAfterInitialization方法,我们就不做任何处理。
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean instanceof ApplicationListener) {
// potentially not detected as a listener by getBeanNamesForType retrieval
Boolean flag = this.singletonNames.get(beanName);
if (Boolean.TRUE.equals(flag)) {
// singleton bean (top-level or inner): register on the fly
this.applicationContext.addApplicationListener((ApplicationListener>) bean);
}
else if (Boolean.FALSE.equals(flag)) {
if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) {
// inner bean with other scope - can't reliably process events
logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " +
"but is not reachable for event multicasting by its containing ApplicationContext " +
"because it does not have singleton scope. Only top-level listener beans are allowed " +
"to be of non-singleton scope.");
}
this.singletonNames.remove(beanName);
}
}
return bean;
}
总结:BeanPostProcessor这个接口其实就是Spring容器中bean对象初始化途中,进行的初始化化自身,或者初始化当前容器的属性或者其他对象的属性等。
这里的监听者对象,其实就是容器中的事件接收者。下一篇我们进行深入分析一下spring中的事件处理,自定义事件,自定义监听者。