refresh - prepareBeanFactory(beanFactory)

Series Article Directory

Spring 5.2.9 source code



Preface

prepareBeanFactory(beanFactory)

For some preparations, two BPPs were added: ApplicationContextAwareProcessor, ApplicationListenerDetector
sets up interfaces that ignore auto-wiring and allow auto-wiring, if some beans do not exist, automatically register and
set EL expression parser, etc.

Reference: yu_kang 's spring loading process refresh of 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());
	}
}

Set the context class loader

// Tell the internal bean factory to use the context's class loader etc.
// 设置上下文类加载器
beanFactory.setBeanClassLoader(getClassLoader());

Entering into DefaultResourceLoader, the parent class of AbstractApplicationContext, through the inheritance implementation relationship of AnnotationConfigApplicationContext, it can be known that it inherits from DefaultResourceLoader, so when it is initialized, it should have called the parameterless constructor of this class and initialized the default ClassLoader

@Override
@Nullable
public ClassLoader getClassLoader() {
    
    
	return (this.classLoader != null ? this.classLoader : ClassUtils.getDefaultClassLoader());
}
public DefaultResourceLoader() {
    
    
	this.classLoader = ClassUtils.getDefaultClassLoader();
}

Set up the bean expression interpreter

// 添加 Bean 表达式解析器(即 SpEL 表达式), 默认以前缀 “#{” 开头, 以后缀 “}” 结尾, 可自定义前后缀
// 在 Bean 的生产过程中, 填充属性的 populateBean 方法会使用到, 解析 @Value 等注解内的 EL 表达式, @Value("#{}") 与 @Value("${}")
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));

Add property editor support

What is the property editor ?

// 添加属性编辑器的支持, 在属性注入的时候, 能够将配置的 String 类型的值转为 Bean 属性真正的类型
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

Add 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);

This BPP covers the postProcessBeforeInitialization method executed before Bean initialization, and executes its set method for 6 kinds of Aware

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);
	}
}

Register several automatic assembly rules

// 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);

Add BPP ApplicationListenerDetector

// Register early post-processor for detecting inner beans as ApplicationListeners.
// 添加一个 BPP, ApplicationListenerDetector,
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

ApplicationListenerDetector implements postProcessAfterInitialization and postProcessBeforeDestruction, if the Bean is an application event listener ApplicationListener, it is registered to the application's event multicaster and removed respectively

Add BPP LoadTimeWeaverAwareProcessor

LoadTimeWeaver LTW is a weaving aspect during the loading of the class bytecode into the JVM, as well as compile-time weaving and runtime weaving (CGLib and JDK dynamic proxy)

// 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;
}

Add default environment 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());
}

Guess you like

Origin blog.csdn.net/mrathena/article/details/109102087