文章目录
前言
prepareBeanFactory(beanFactory)
一些准备工作, 添加了两个 BPP: ApplicationContextAwareProcessor, ApplicationListenerDetector
设置了 忽略自动装配 和 允许自动装配 的接口, 如果不存在某些 Bean 的时候, 自动注册
设置了 EL 表达式解析器 等
参考: yu_kang 老哥的 spring加载流程refresh之prepareBeanFactory(beanFactory)
prepareBeanFactory(beanFactory)
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// Tell the internal bean factory to use the context's class loader etc.
// 设置上下文类加载器
beanFactory.setBeanClassLoader(getClassLoader());
/*
* 添加 Bean 表达式解析器(即 SpEL 表达式), 默认以前缀 “#{” 开头, 以后缀 “}” 结尾, 可自定义前后缀
* 在 Bean 的生产过程中, 填充属性的 populateBean 方法会使用到, 解析 @Value 等注解内的 EL 表达式
*/
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
// 添加属性编辑器的支持, 在属性注入的时候, 能够将配置的 String 类型的值转为 Bean 属性真正的类型
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
// Configure the bean factory with context callbacks.
// 添加一个 BPP, ApplicationContextAwareProcessor, 在 Bean 创建过程中被调用, 调用下面 6 种 Aware 的 set 方法
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
// 忽略如下 6 种 Aware 的自动装配, 测试发现, 如果注释了上一行且注释了下面的 Aware, 无法自动注入属性
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
// BeanFactory interface not registered as resolvable type in a plain factory.
// MessageSource registered (and found for autowiring) as a bean.
// 某个 Bean 中包含如下 4 种类型的属性时, 会被自动赋值, 即使这些类型的 Bean 并没有被显式地定义
// 第一个参数是属性类型, 第二个值是属性值
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
// Register early post-processor for detecting inner beans as ApplicationListeners.
// 添加一个 BPP, ApplicationListenerDetector,
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
// Detect a LoadTimeWeaver and prepare for weaving, if found.
// 当存在名为 loadTimeWeaver 的 Bean 时, 添加一个 BPP, LoadTimeWeaverAwareProcessor
// 如果这个 Bean 实现了 LoadTimeWeaverAware, 该 BPP 就会调用这个 Aware 的 set 方法
if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
// Set a temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
// Register default environment beans.
// 如果本地容器(不从上下文父子容器中找)中没有自定义名为 environment,systemProperties,systemEnvironment 的 Bean, 则自动注册这些 Bean
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}
}
设置上下文类加载器
// Tell the internal bean factory to use the context's class loader etc.
// 设置上下文类加载器
beanFactory.setBeanClassLoader(getClassLoader());
进入到 AbstractApplicationContext 的父类 DefaultResourceLoader 中, 通过 AnnotationConfigApplicationContext 的继承实现关系可知, 其继承于 DefaultResourceLoader, 所以其初始化的时候应该已经调用过该类的无参构造函数, 已经初始化过默认的 ClassLoader
@Override
@Nullable
public ClassLoader getClassLoader() {
return (this.classLoader != null ? this.classLoader : ClassUtils.getDefaultClassLoader());
}
public DefaultResourceLoader() {
this.classLoader = ClassUtils.getDefaultClassLoader();
}
设置 Bean 表达式解释器
// 添加 Bean 表达式解析器(即 SpEL 表达式), 默认以前缀 “#{” 开头, 以后缀 “}” 结尾, 可自定义前后缀
// 在 Bean 的生产过程中, 填充属性的 populateBean 方法会使用到, 解析 @Value 等注解内的 EL 表达式, @Value("#{}") 与 @Value("${}")
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
添加属性编辑器支持
什么是 属性编辑器 ?
// 添加属性编辑器的支持, 在属性注入的时候, 能够将配置的 String 类型的值转为 Bean 属性真正的类型
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
添加 BPP ApplicationContextAwareProcessor
// Configure the bean factory with context callbacks.
// 添加一个 BPP, ApplicationContextAwareProcessor, 在 Bean 创建过程中被调用, 调用下面 6 种 Aware 的 set 方法
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
// 忽略如下 6 种 Aware 的自动装配, 测试发现, 如果注释了上一行且注释了下面的 Aware, 无法自动注入属性
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
该 BPP 覆盖了 Bean 初始化前执行的 postProcessBeforeInitialization 方法, 对 6 种 Aware 执行其 set 方法
private void invokeAwareInterfaces(Object bean) {
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);
}
}
注册几个自动装配规则
// BeanFactory interface not registered as resolvable type in a plain factory.
// MessageSource registered (and found for autowiring) as a bean.
// 某个 Bean 中包含如下 4 种类型的属性时, 会被自动赋值, 即使这些类型的 Bean 并没有被显式地定义
// 第一个参数是属性类型, 第二个值是属性值
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
添加 BPP ApplicationListenerDetector
// Register early post-processor for detecting inner beans as ApplicationListeners.
// 添加一个 BPP, ApplicationListenerDetector,
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
ApplicationListenerDetector 实现了 postProcessAfterInitialization 和 postProcessBeforeDestruction, 如果该 Bean 是一个 应用事件监听器 ApplicationListener, 分别实现了 将其注册到应用的事件多播器上 和 移除
添加 BPP LoadTimeWeaverAwareProcessor
LoadTimeWeaver LTW 是类字节码加载到JVM期间织入切面, 还有编译期织入和运行期织入(CGLib和JDK动态代理)
// Detect a LoadTimeWeaver and prepare for weaving, if found.
// 当存在名为 loadTimeWeaver 的 Bean 时, 添加一个 BPP, LoadTimeWeaverAwareProcessor
// 如果这个 Bean 实现了 LoadTimeWeaverAware, 该 BPP 就会调用这个 Aware 的 set 方法
if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
// Set a temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof LoadTimeWeaverAware) {
LoadTimeWeaver ltw = this.loadTimeWeaver;
if (ltw == null) {
Assert.state(this.beanFactory != null,
"BeanFactory required if no LoadTimeWeaver explicitly specified");
ltw = this.beanFactory.getBean(
ConfigurableApplicationContext.LOAD_TIME_WEAVER_BEAN_NAME, LoadTimeWeaver.class);
}
((LoadTimeWeaverAware) bean).setLoadTimeWeaver(ltw);
}
return bean;
}
添加默认环境 Bean
// Register default environment beans.
// 如果本地容器(不从上下文父子容器中找)中没有自定义名为 environment,systemProperties,systemEnvironment 的 Bean, 则自动注册这些 Bean
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}