Spring源码之四prepareBeanFactory()方法

Spring源码之四prepareBeanFactory()方法

大家好,我是程序员田同学!

今天带大家解读refresh()方法的第三个方法prepareBeanFactory(),通过对refresh()的一步步解读,想必有一天小伙伴们能揭开Spring的神秘面纱。

照例,先站在prepareBeanFactory()的门外看它都做了什么,然后再逐步深入剖析。prepareBeanFactory()方法主要是:

设置 BeanFactory 的类加载器,添加 BeanPostProcessor,手动注册几个特殊的 bean。

扩展:BeanPostProcessor是Spring IOC容器给我们提供的一个扩展接口。

1
2
3
4
5
6
7
8
9
10
11
//bean初始化方法调用前被调用
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}

//bean初始化方法调用后被调用
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}

BeanPostProcessor在整个bean的生命周期中处于红色箭头所示的位置。

image-20220119093428999

prepareBeanFactory()的每个详细方法在以下代码注释中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Tell the internal bean factory to use the context's class loader etc.
//设置上下文环境的启动类加载器
beanFactory.setBeanClassLoader(getClassLoader());

//添加bean表达式解释器,为了能够让我们的beanFactory去解析bean表达式
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));

//spring内部的属性编辑器、既读取配置文件
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

// Configure the bean factory with context callbacks.
//添加BeanPostProcessor
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

//跳过以下6个属性的自动注入
//因为在ApplicationContextAwareProcessor后置处理器中通过setter注入
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
//在应用代码就可以通过类型自动装配把工厂实例和ApplicationContext实例设置到自定义bean的属性中
//这四个属性都会被自动设置,虽然没有在显示的在bean定义xml中注入它们
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.
//注册事件监听器
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));


// Detect a LoadTimeWeaver and prepare for weaving, if found.
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" 这个 bean,那么 Spring 会 "手动" 注册一个
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
// 如果没有定义 "systemProperties" 这个 bean,那么 Spring 会 "手动" 注册一个
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
// 如果没有定义 "systemEnvironment" 这个 bean,那么 Spring 会 "手动" 注册一个
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}

有一些同学想必对ignoreDependencyInterface()方法有些许的疑惑。

ignoreDependencyInterface的主要功能是忽略给定接口的自动装配功能,也就是当有忽略的接口类,自动装配会忽略这部分类的初始化装配,因为某种情况下,此时的接口实现类不能初始化。

例如BeanNameAware,要想装配这个接口的实现对象,可以实现这个接口,通过实现的set方法进行装配

安利时刻:

image-20220714114938715


Spring源码之四prepareBeanFactory()方法
http://www.sky1998.cn/2022/03/17/Spring/Spring源码之四prepareBeanFactory()方法/
作者
程序员田同学
发布于
2022年3月17日
许可协议