Spring——5. ApplicationContext容器的功能扩展

文章目录

  • 1. XML配置文件的读取
    • 1.1 ApplicationContext配置文件的读取
      • 1.1.1 设置配置路径
  • 2. 扩展功能
    • 2.1 环境准备
      • 2.1.1 initPropertySources
      • 2.1.2 validateRequiredProperties
    • 2.2 初始化BeanFactory
      • 2.2.1 定制BeanFactory
      • 2.2.2 加载BeanDefinition
    • 2.3 正式扩展功能
      • 2.3.1 增加SpEL语言的支持
      • 2.3.2 增加属性注册编辑器
      • 2.3.3 增加 ApplicationContextAwareProcessor处理器
      • 2.3.4 忽略依赖
      • 2.3.5 注册依赖
    • 2.4 BeanFactory的后处理
      • 2.4.1 激活注册的 BeanFactoryPostProcessor
        • 2.4.1.1 BeanFactoryPostProcessor的使用
        • 2.4.1.2 激活(调用) BeanFactoryPostProcessor
      • 2.4.2 注册BeanPostProcessor
      • 2.4.3 初始化消息资源
      • 2.4.4 初始化 ApplicationEventMulticaster
      • 2.4.5 注册监听器到广播器中
    • 2.5 完成BeanFactory的剩余初始化工作
      • 2.5.1 ConversionService的设置
      • 2.5.2 冻结BeanDefinition
      • 2.5.3 初始化非延迟加载单例bean
    • 2.6 finishRefresh
      • 2.6.1 initLifecycleProcessor
      • 2.6.2 onRefresh
      • 2.6.3 publishEvent
  • 3. 整体流程思维导图

ApplicationContext容器的初始化

在Spring——1. BeanFactory容器的初始化中提到过,ApplicationContext容器的初始化实现为:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("org/springframework/example/application-context-demo.xml");

下面来看看具体的实现。

1. XML配置文件的读取

1.1 ApplicationContext配置文件的读取

new ClassPathXmlApplicationContext("org/springframework/example/application-context-demo.xml");
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
	this(new String[] {configLocation}, true, null);
}

public ClassPathXmlApplicationContext(
		String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
		throws BeansException {
	super(parent);
	// 用于解析配置的xml信息,将xml路径信息赋值给configLocations,是一个String[](xml可以传入多个),
	setConfigLocations(configLocations);
	if (refresh) {
		// 进行初始化
		refresh();
	}
}

ClassPathXmlApplicationContext中可以将配置文件路径以数组的方式传入,然后对数组进行解析并进行加载,解析和加载都在refresh()中实现。

1.1.1 设置配置路径

public void setConfigLocations(@Nullable String... locations) {
	if (locations != null) {
		Assert.noNullElements(locations, "Config locations must not be null");
		this.configLocations = new String[locations.length];
		for (int i = 0; i < locations.length; i++) {
			this.configLocations[i] = resolvePath(locations[i]).trim();
		}
	}
	else {
		this.configLocations = null;
	}
}
这里主要是解析给定的路径数组;如果路径中包含一些占位符如${var},会在resolvePath()方法中搜寻匹配的系统变量并替换。

2. 扩展功能

ApplicationContext跟BeanFactory不同之处就在于,ApplicationContext提供了更多的扩展功能;而这些多出来的扩展功能的实现就在refresh()方法中;并且在refresh()方法中根据读取的配置文件,进行ApplicationContext的初始化。

public void refresh() throws BeansException, IllegalStateException {
	synchronized (this.startupShutdownMonitor) {
		// Prepare this context for refreshing.
		// 准备刷新的上下文环境
		prepareRefresh();
		// Tell the subclass to refresh the internal bean factory.
		// 进行xml文件的读取,并初始化BeanFactory
		// ApplicationContext包含BeanFactory的所有特征,这里将会复用BeanFactory中的配置文件的读取解析及其他功能
		// 在执行完 obtainFreshBeanFactory之后,ApplicationContext就拥有了 BeanFactory的全部功能(也包含了解析好的xml配置文件)
		ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
		// Prepare the bean factory for use in this context.
		// 对BeanFactory进行各种功能的填充,即ApplicationContext在功能上的扩展
		prepareBeanFactory(beanFactory);
		try {
			// Allows post-processing of the bean factory in context subclasses.
			// 允许子类覆盖这个方法对BeanFactory做额外的处理(这里是空方法没有具体实现)
			postProcessBeanFactory(beanFactory);
			// Invoke factory processors registered as beans in the context.
			// 激活(调用)各种BeanFactory的处理器;
			// 作用:直接调用;在bean实例化之前,修改bean的各种属性
			invokeBeanFactoryPostProcessors(beanFactory);
			// Register bean processors that intercept bean creation.
			// 注册 拦截bean创建 的bean处理器
			// 这里是注册,调用是在bean实例化阶段进行的;
			// 这是一个很重要的功能(Spring中大部分功能都是通过后处理器的方式进行扩展的),但是在BeanFactory中并没有实现后处理器的注册;
			// 在调用的时候如果没有进行手动注册是不能使用的,所以这也是BeanFactory不支持很多功能的原因;
			registerBeanPostProcessors(beanFactory);
			// Initialize message source for this context.
			// 国际化处理,为上下文初始化不同的message源;
			// 这里通过读取并将“自定义资源文件”配置(messageSource的bean,它会去读取用户配置的message.properties文件)
			// 记录在容器中,那么就可以在获取资源文件的时候直接使用了;
			initMessageSource();
			// Initialize event multicaster for this context.
			// 初始化 应用消息广播器(广播事件,调用监听器的 onApplicationEvent方法来进行监听)
			initApplicationEventMulticaster();
			// Initialize other special beans in specific context subclasses.
			// 留给子类来初始化其他的bean
			onRefresh();
			// Check for listener beans and register them.
			// 查找 listener 注册到消息广播器中
			registerListeners();
			// Instantiate all remaining (non-lazy-init) singletons.
			// 初始化非延迟加载单例
			finishBeanFactoryInitialization(beanFactory);
			// Last step: publish corresponding event.
			// 完成刷新过程,通知生命周期处理器,同时发出 ContextRefreshedEvent通知别人
			finishRefresh();
		}
		catch (BeansException ex) {
			if (logger.isWarnEnabled()) {
				logger.warn("Exception encountered during context initialization - " +
						"cancelling refresh attempt: " + ex);
			}
			// Destroy already created singletons to avoid dangling resources.
			destroyBeans();
			// Reset 'active' flag.
			cancelRefresh(ex);
			// Propagate exception to caller.
			throw ex;
		}
		finally {
			// Reset common introspection caches in Spring's core, since we
			// might not ever need metadata for singleton beans anymore...
			resetCommonCaches();
		}
	}
}

refresh()方法的主要流程为:

  1. 初始化前的环境准备工作,例如对系统属性或者环境变量进行准备和验证;
  2. 初始化BeanFactory,并进行xml文件的读取,这里复用了BeanFactory的配置文件读取解析过程,相当于进行了一次BeanFactory的初始化;
  3. 对BeanFactory进行各种扩展和属性填充;(如@Qualifier与@Autowired注解的支持)
  4. 子类可覆盖父类的方法,进行额外的后置处理(postProcessBeanFactory是空方法,由子类进行扩展);
  5. 激活各种BeanFactory处理器;
  6. 注册拦截bean创建的bean处理器;(这里只是注册,真正的调用是在getBean()时);
  7. 为上下文初始化Message源;(即对不同语言的消息体进行国际化处理)
  8. 初始化应用消息广播器,并放入 applicationEventMulticaster(应用消息广播器) 的bean中;
  9. 留给子类来初始化其他的bean;
  10. 在所有注册的bean中查找listener bean,注册到消息广播器中;
  11. 初始化非延迟加载的单例bean;
  12. 完成刷新过程,通知生命周期处理器 LifecycleProcessor 刷新过程,同时发出 ContextRefreshedEvent 通知别人;

2.1 环境准备

protected void prepareRefresh() {
	// Switch to active.
	this.startupDate = System.currentTimeMillis();
	this.closed.set(false);
	this.active.set(true);
	if (logger.isDebugEnabled()) {
		if (logger.isTraceEnabled()) {
			logger.trace("Refreshing " + this);
		}
		else {
			logger.debug("Refreshing " + getDisplayName());
		}
	}
	// Initialize any placeholder property sources in the context environment.
	// Spring的开放式结构设计,让用户可以根据自身的需要重写这个方法,在其中进行个性化的属性处理及设置
	initPropertySources();
	// Validate that all properties marked as required are resolvable:
	// see ConfigurablePropertyResolver#setRequiredProperties
	// 可以在容器启动时,添加验证要求:getEnvironment.setRequiredProperties("var");
	getEnvironment().validateRequiredProperties();
	// Store pre-refresh ApplicationListeners...
	if (this.earlyApplicationListeners == null) {
		this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
	}
	else {
		// Reset local application listeners to pre-refresh state.
		this.applicationListeners.clear();
		this.applicationListeners.addAll(this.earlyApplicationListeners);
	}
	// Allow for the collection of early ApplicationEvents,
	// to be published once the multicaster is available...
	this.earlyApplicationEvents = new LinkedHashSet<>();
}

这里主要就是做些准备工作,例如对系统属性及环境变量的初始化及验证。

2.1.1 initPropertySources

protected void initPropertySources() {
	// For subclasses: do nothing by default.
}

这是一个空方法,符合Spring的开放式结构设计,给用户最大扩展Spring的能力;用户可以根据自身的需要在子类中重写initPropertySources方法,在方法中进行个性化的属性处理及配置;

2.1.2 validateRequiredProperties

validateRequiredProperties()方法是对属性进行验证;
示例:
某个项目中用到的一个设置属性(如VAR)是从系统环境变量中取得的,如果用户没有在系统环境变量中配置这个参数,那么项目将不会正常工作,所以需要先对这个变量进行验证;我们可以对Spring进行扩展来实现这个功能:

  1. 自定义类:
public class MyClassPathXmlApplicationContext extends ClassPathXmlApplicationContext {
	public MyClassPathXmlApplicationContext(String... configLocations) throws BeansException {
		super(configLocations);
	}

	@Override
	protected void initPropertySources() {
		getEnvironment().setRequiredProperties("VAR");
	}
}

继承ClassPathXmlApplicationContext类,并重写initPropertySources()方法,在这个方法中添加了我们需要验证的属性“VAR”的环境变量;这个时候就将会调用Spring中的 getEnvironment().validateRequiredProperties() 方法进行验证,如果系统没有检测到对应VAR的环境变量,将抛出异常。
2. 使用自定义ApplicationContext获取bean:

public class ApplicationContextTestDemo {
	public static void main(String[] args) {
		ApplicationContext applicationContext = new MyClassPathXmlApplicationContext("classpath:property-context.xml");
		People people = (People)applicationContext.getBean("people");
		System.out.println(people);
	}
}
  1. 抛出异常:
Exception in thread "main" org.springframework.core.env.MissingRequiredPropertiesException: The following properties were declared as required but could not be resolved: [VAR]
	at org.springframework.core.env.AbstractPropertyResolver.validateRequiredProperties(AbstractPropertyResolver.java:145)
	at org.springframework.core.env.AbstractEnvironment.validateRequiredProperties(AbstractEnvironment.java:521)
	at org.springframework.context.support.AbstractApplicationContext.prepareRefresh(AbstractApplicationContext.java:604)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:519)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:95)
	at org.bgy.spring.study.spring.bean.definition.application.context.MyClassPathXmlApplicationContext.<init>(MyClassPathXmlApplicationContext.java:12)
	at org.bgy.spring.study.spring.bean.definition.application.context.ApplicationContextTestDemo.main(ApplicationContextTestDemo.java:12)
Disconnected from the target VM, address: '127.0.0.1:52137', transport: 'socket'

2.2 初始化BeanFactory

obtainFreshBeanFactory()方法的字面意思就是获取BeanFactory;之前说过,ApplicationContext是对BeanFactory功能的扩展,不但包含了BeanFactory的全部功能,更在它的基础上添加了大量的扩展应用。
obtainFreshBeanFactory()方法正是实现拥有BeanFactory本身功能的地方;

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
	// 初始化BeanFactory,并进行XML文件读取,并将得到的BeanFactory记录在当前实体的属性中
	refreshBeanFactory();
	return getBeanFactory();
}

将核心的实现委托给了refreshBeanFactory()方法。

AbstractRefreshableApplicationContext.java

protected final void refreshBeanFactory() throws BeansException {
	if (hasBeanFactory()) {
		destroyBeans();
		closeBeanFactory();
	}
	try {
		// 创建 DefaultListableBeanFactory
		DefaultListableBeanFactory beanFactory = createBeanFactory();
		// 为了序列化指定id
		beanFactory.setSerializationId(getId());
		// 定制BeanFactory,设置相关属性,
		customizeBeanFactory(beanFactory);
		// 进行xml配置文件的读取及解析,封装成BeanDefinition
		loadBeanDefinitions(beanFactory);
		this.beanFactory = beanFactory;
	}
	catch (IOException ex) {
		throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
	}
}

主要流程为:

  1. 创建DefaultListableBeanFactory对象;
    在Spring——1. BeanFactory容器的初始化中的BeanFactory的初始化为: new XmlBeanFactory(…); 其中的 XmlBeanFactory 继承自 DefaultListableBeanFactory 对象,也就是说 DefaultListableBeanFactory 是容器的基础,必须首先实例化,所以在这里也就是实例化 DefaultListableBeanFactory。
  2. 指定序列号id;
  3. 定制BeanFactory;
  4. 加载BeanDefinitions;
  5. 记录BeanFactory;

这里主要来看看第3,4步:

2.2.1 定制BeanFactory

其实在这里就已经开始了对BeanFactory的扩展,在BeanFactory容器的基础上,增加了“是否允许覆盖”、“是否允许扩展”的设置;

protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
	// 这两个属性的值,通过子类覆盖方法来进行设置
	// 是否允许覆盖同名称的不同定义的对象
	if (this.allowBeanDefinitionOverriding != null) {
		beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
	}
	// 是否允许bean之间存在循环依赖
	if (this.allowCircularReferences != null) {
		beanFactory.setAllowCircularReferences(this.allowCircularReferences);
	}
}

这里只是判断了是否为空,如果不为空需要进行属性的设置;但是这个不为空是在哪里设置进去的呢?其实跟这里一样,通过子类来重写方法实现:

public class MyClassPathXmlApplicationContext extends ClassPathXmlApplicationContext {
	public MyClassPathXmlApplicationContext(String... configLocations) throws BeansException {
		super(configLocations);
	}

	@Override
	protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
		super.setAllowBeanDefinitionOverriding(false);
		super.setAllowCircularReferences(false);
		super.customizeBeanFactory(beanFactory);
	}
}

在这里设置之后,在ApplicationContext初始化的时候,就会进行判断并且设置BeanFactory的属性值。

2.2.2 加载BeanDefinition

AbstractXmlApplicationContext.java

protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
	// Create a new XmlBeanDefinitionReader for the given BeanFactory.
	// 使用 XmlBeanDefinitionReader 来读取xml
	XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
	// Configure the bean definition reader with this context's
	// resource loading environment.
	// 对beanDefinitionReader进行环境变量设置
	beanDefinitionReader.setEnvironment(this.getEnvironment());
	beanDefinitionReader.setResourceLoader(this);
	beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
	// Allow a subclass to provide custom initialization of the reader,
	// then proceed with actually loading the bean definitions.
	// 允许子类提供对beanDefinitionReader的自定义设置
	initBeanDefinitionReader(beanDefinitionReader);
	loadBeanDefinitions(beanDefinitionReader);

在第一步中创建了DefaultListableBeanFactory对象之后,还需要 XmlBeanDefinitionReader来读取XML,在这个步骤中首先就是创建并初始化XmlBeanDefinitionReader;

然后再由XmlBeanDefinitionReader去加载BeanDefinitions:

protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
	Resource[] configResources = getConfigResources();
	if (configResources != null) {
		reader.loadBeanDefinitions(configResources);
	}
	String[] configLocations = getConfigLocations();
	if (configLocations != null) {
		reader.loadBeanDefinitions(configLocations);
	}
}

在这里先判断到Resource[]有没有被配置,并且
在这里配置的是configLocations,所以会走 reader.loadBeanDefinitions(configLocations);

AbstractBeanDefinitionReader

public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
	Assert.notNull(locations, "Location array must not be null");
	int count = 0;
	for (String location : locations) {
		// 遍历locations,对每一个location进行loadBeanDefinitions;
		count += loadBeanDefinitions(location);
	}
	return count;
}

跟着走下去:

public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
	return loadBeanDefinitions(location, null);
}

public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanD
	ResourceLoader resourceLoader = getResourceLoader();
	if (resourceLoader == null) {
		throw new BeanDefinitionStoreException(
				"Cannot load bean definitions from location [" + location + "]: no ResourceLoader ava
	}
	// 通过调试,看到 resourceLoader为 org.springframework.context.support.ClassPathXmlApplicationContext@3b0143d3
	if (resourceLoader instanceof ResourcePatternResolver) {
		// Resource pattern matching available.
		// instanceof 匹配上了,通过ResourcePatternResolver的getResources()方法获取Resource[]
		try {
			Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
			int count = loadBeanDefinitions(resources);
			if (actualResources != null) {
				Collections.addAll(actualResources, resources);
			}
			if (logger.isTraceEnabled()) {
				logger.trace("Loaded " + count + " bean definitions from location pattern [" + locati
			}
			return count;
		}
		catch (IOException ex) {
			throw new BeanDefinitionStoreException(
					"Could not resolve bean definition resource pattern [" + location + "]", ex);
		}
	}
	else {
		// Can only load single resources by absolute URL.
		Resource resource = resourceLoader.getResource(location);
		int count = loadBeanDefinitions(resource);
		if (actualResources != null) {
			actualResources.add(resource);
		}
		if (logger.isTraceEnabled()) {
			logger.trace("Loaded " + count + " bean definitions from location [" + location + "]");
		}
		return count;
	}
}

在调用ResourcePatternResolver的getResources()方法获取到Resource[]之后,加载BeanDefinitions:int count = loadBeanDefinitions(resources);

public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
	Assert.notNull(resources, "Resource array must not be null");
	int count = 0;
	for (Resource resource : resources) {
		// 遍历resources,一个一个的去加载
		count += loadBeanDefinitions(resource);
	}
	return count;
}

BeanDefinitionReader

int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException;

我们的实现类是XmlBeanDefinitionReader,所以顺利的来到了XmlBeanDefinitionReader的loadBeanDefinitions(Resource resource)方法,这里就跟Spring——1. BeanFactory容器的初始化的2.1小节结合起来了:

public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
	// 对Resource,使用EncodedResource进行封装(主要是考虑到Resource可能存在编码要求)
	return loadBeanDefinitions(new EncodedResource(resource));
}

2.3 正式扩展功能

在进入prepareBeanFactory()方法前,Spring已经完成了对XML配置文件的解析,ApplicationContext对于BeanFactory在功能上的扩展也由此开始:

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
	// Tell the internal bean factory to use the context's class loader etc.
	beanFactory.setBeanClassLoader(getClassLoader());
	// 设置BeanFactory的表达式语言处理器,这里是标准的SpelExpressionParser
	beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
	// 设置属性编辑器
	beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
	// Configure the bean factory with context callbacks.
	// 添加Aware的 BeanPostProcessor
	beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
	// 设置几个忽略自动装配的接口(忽略依赖)
	// (因为在注册了 ApplicationContextAwareProcessor之后,在invokeAwareInterfaces()方法中的这些Aware类就不再是普通的bean了;
	// 如 EnvironmentAware、ApplicationContextAware等;需要在Spring做bean的依赖注入时忽略它们;
	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.
	// 设置几个自动装配的特殊规则(注册依赖)
	// 当注册了 BeanFactory.class的解析依赖之后,当bean的属性注入的时候,一旦检测到属性为BeanFactory类,就会将这个 beanFactory的实例注入进去
	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.
	// 添加监听器的 BeanPostProcessor
	beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
	// Detect a LoadTimeWeaver and prepare for weaving, if found.
	// 监听LoadTimeWeaver和准备织入,增加对 AspectJ的支持
	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.
	// 注册默认的系统环境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());
	}
}

主要实现了以下几个方面的扩展:

  1. 增加了对SpEL语言的支持;
  2. 增加了对属性编辑器的支持;
  3. 增加了对一些内置类(EnvironmentAware、MessageSourceAware)的信息注入;
  4. 设置了依赖功能可忽略的接口;
  5. 注册了一些固定依赖的属性;
  6. 增加了AspectJ的支持;
  7. 将相关环境变量及属性以单例模式注册;

2.3.1 增加SpEL语言的支持

SpEL语言全称为Spring表达式语言(Spring Expression Language),能够在运行时构建复杂表达式,存取对象图属性,对象方法调用等,并且能与Spring功能完美整合,比如能用来配置bean定义。

SpEL使用#{…}作为定界符,所有在大括号中的字符都将被认为是SpEL;使用示例:

<bean id="saxophone" value="com.xxx.xxx"/>
<bean>
    <property name="instrument" value="#{saxophone}"/>
bean>

相当于:

<bean id="saxophone" value="com.xxx.xxx"/>
<bean>
    <property name="instrument" ref="saxophone"/>
bean>

回到源码:

// 设置BeanFactory的表达式语言处理器,这里是标准的SpelExpressionParser
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));

在这里注册语言解析器,就可以对SpEL进行解析了;那注册解析器之后,什么时候调用这个解析器进行解析呢?

前面的文章提到过,Spring在bean进行初始化的时候,会有属性填充的步骤;在这个步骤中Spring过会调用AbstractAutowireCapableBeanFactory类的applyPropertyValues方法来完成解析的动作。

2.3.2 增加属性注册编辑器

在Spring依赖注入的时候可以把普通属性注入进去,但是有些类型可能无法识别,比如Date类型;例如:

public class UserManager {
    private Date date;
}

<bean id="userManager" class="com.xxx.xxx">
    <property name="date" value="2020-10-10"/>
</bean

如果直接这样进行注册,程序会报类型转换不成功的异常,因为在类中的date是Date类型的,而XML中的date是String类型的。

Spring针对此问题提供了两种解决方法:

  1. 自定义属性编辑器
  2. 注册Spring自带的属性编辑器 CustomDateFormat
    1. 定义属性编辑器:
      public class DatePropertyEditorRegister implements PropertyEditorRegistrar {
      	@Override
      	public void registerCustomEditors(PropertyEditorRegistry registry) {
      		registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-mm-dd"), true));
      	}
      }
      
    2. 注册到Spring中:
      
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
              https://www.springframework.org/schema/beans/spring-beans.xsd">
      
          <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
              <property name="propertyEditorRegistrars">
                  <list>
                      <bean class="org.bgy.spring.study.spring.bean.definition.custom.editor.DatePropertyEditorRegister"/>
                  list>
              property>
          bean>
      beans>
      
      通过在配置文件中将自定义的DatePropertyEditorRegister注册进入 org.springframework.beans.factory.config.CustomEditorConfigurer中的 propertyEditorRegistrars属性中;

回到源码:

// 设置属性编辑器
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

跟进查看ResourceEditorRegistrar的内部实现,在这个类中我们关注的是registrCustomEditors()方法:

ResourceEditorRegistrar.java

public void registerCustomEditors(PropertyEditorRegistry registry) {
	ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);
	doRegisterEditor(registry, Resource.class, baseEditor);
	doRegisterEditor(registry, ContextResource.class, baseEditor);
	doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
	doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
	doRegisterEditor(registry, File.class, new FileEditor(baseEditor));
	doRegisterEditor(registry, Path.class, new PathEditor(baseEditor));
	doRegisterEditor(registry, Reader.class, new ReaderEditor(baseEditor));
	doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));
	ClassLoader classLoader = this.resourceLoader.getClassLoader();
	doRegisterEditor(registry, URI.class, new URIEditor(classLoader));
	doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));
	doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));
	if (this.resourceLoader instanceof ResourcePatternResolver) {
		doRegisterEditor(registry, Resource[].class,
				new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));
	}
}
private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) {
	if (registry instanceof PropertyEditorRegistrySupport) {
		((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor);
	}
	else {
		registry.registerCustomEditor(requiredType, editor);
	}
}

在doRegisterEditor()方法中,我们可以看到自定义属性中使用的关键代码。

回过头来看 ResourceEditorRegistrar类的 registerCustomEditors()方法的核心功能就是:注册了一系列的常用类型的属性编辑器。

例如:doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));实现的功能就注册Class类对应的属性编辑器,在注册后,如果某个实体bean中存在Class类型的属性,那么Spring会调用ClassEditor将配置中定义的String类型转换为Class类型并进行赋值。

registerCustomEditors()方法虽然实现了批量注册editors的功能,但是在上面的源码中仅仅是注册了ResourceEditorRegistrar的实例,并没有调用registerCustomEditors()方法,那么这个批量注册的方法是在哪里进行调用的呢?

通过查看registerCustomEditors()方法的调用层次,我们最终找到了在 AbstractBeanFactory类中 initBeanWrapper()方法会去间接调用;这是在bean初始化时使用的一个方法,所以在bean初始化后会调用 ResourceEditorRegistrar的 registerCustomEditors()方法进行批量的通用属性编辑器的注册;注册后,在属性填充的环节便可以直接让Spring使用这些编辑器进行属性的解析了。

2.3.3 增加 ApplicationContextAwareProcessor处理器

// 添加Aware的 BeanPostProcessor
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));

ApplicationContextAwareProcessor实现了 BeanPostProcessor接口,在Spring——4. Bean的加载(二)中的 2.4.2 小节中我们讲到过:在bean实例化之后,Spring激活bean的init-method的前后,会分别调用 BeanPostProcessor的 postProcessBeforeInitialization()方法和 postProcessAfterInitialization()方法。

同样,对于ApplicationContextAwareProcessor我们也关心这两个方法:

// ApplicationContextAwareProcessor中没有重写 postProcessAfterInitialization()方法;

// 重写 postProcessBeforeInitialization()方法
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
	if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
			bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
			bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)){
		return bean;
	}
	AccessControlContext acc = null;
	if (System.getSecurityManager() != null) {
		acc = this.applicationContext.getBeanFactory().getAccessControlContext();
	}
	if (acc != null) {
		AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
			invokeAwareInterfaces(bean);
			return null;
		}, acc);
	}
	else {
		invokeAwareInterfaces(bean);
	}
	return bean;
}
// 实现这些Aware接口的bean在被初始化之后,可以取得一些对应的资源
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);
	}
}

可以看到,在 postProcessBeforeInitialization()方法中调用了 invokeAwareInterfaces()方法;从invokeAwareInterfaces()方法中我们可以看到,主要目的是:实现了这些Aware接口的bean在被初始化之后,可以取得一些对应的资源。

这里其实就是ApplicationContext容器扩展需要的资源的设置,如果和BeanFactory容器共有需要设置的资源,是在Spring——4. Bean的加载(二)中的 2.4.1小节中设置的。

2.3.4 忽略依赖

beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

在这里的这些 XxxAware.class 的类,它们的依赖注入是在上面一个方法中实现的,由Spring主动去回调来进行注入;并且对于这些Aware接口,用户关心的或者说需要的如 setApplicationContext(ApplicationContext applicationContext) 方法中注入的applicationContext的bean,而不是这个Aware接口本身。

所以根本不需要注入 XxxAware.class,所以在这里 Spring直接屏蔽了 XxxAware.class等类的注入,也就是使用 @Autowired等方式注入无效。

2.3.5 注册依赖

beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);

要理解这里,先来了解一下背景:
如果一个接口有多个实现类,这个时候有一个bean依赖了这个接口;如果不指定具体的实现类,Spring将不知道注入哪个实现类。

在我们自己的代码中,可以使用@Primary或者@Qualifier注解来指定具体的实现类;那么在Spring框架中又如果实现呢?比如ApplicationContext 接口有多个实现类,Spring怎么知道注入哪个实现类呢?

这个功能就是在这里实现的,比如在这里一旦检测到注入的属性为ApplicationContext类型,就会将this实例注入进去。

所以我们的代码中也可以这样实现:

public interface InterfaceA {}

public class InterfaceA1 implements InterfaceA {}

public class InterfaceA2 implements InterfaceA {}


@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        //当遇到需要注入InterfaceA.class的地方,直接注入A1的对象
        beanFactory.registerResolvableDependency(InterfaceA.class, new InterfaceA1()); 
    }
}

后面的几个扩展比较简单,就不展开说了。

2.4 BeanFactory的后处理

BeanFactory作为Spring中容器功能的基础,用于存放所有已经加载的bean,为了保证程序上的高扩展性,Spring针对BeanFactory做了大量的扩展,比如 PostProcessor等。

2.4.1 激活注册的 BeanFactoryPostProcessor

2.4.1.1 BeanFactoryPostProcessor的使用

BeanFactoryPostProcessor接口跟BeanPostProcessor接口类似,可以对bean的定义(配置元数据)进行处理。

  • Spring容器允许BeanFactoryPostProcessor在任何其他bean实例化之前读取配置元数据,并有可能修改它。
    BeanFactoryPostProcessor的作用域范围是容器级别的,只和使用的容器有关。
  • 如果是想改变实际的bean实例(例如从配置元数据创建的对象),应当使用BeanPostProcessor。

示例:BeanFactoryPostProcessor的典型应用(PropertyPlaceholderConfigurer):

在Spring的XML文件中,注入bean时,可能会使用变量值:

<bean id="helloMessage" class="org.bgy.spring.study.spring.bean.definition.bean.factory.post.processor.HelloMessage">
    <property name="message" value="${bean.message}"/>
bean>

这个变量引用${bean.message},就是Spring的分散配置,可以在另外的配置文件中为bean.message配置值,如在 bean.properties文件中配置如下:

bean.properties

bean.message=Hello World.

当访问 helloMessage的bean时,message属性的值就会被替换为bean.properties文件中的Hello World.;但是Spring怎么知道有这个bean.properties文件呢?
这就需要靠 PropertyPlaceholderConfigurer类的bean了:

<bean id="msgHandler" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>config/bean.propertiesvalue>
        list>
    property>
bean>

在这个bean中指定了配置文件为 config/bean.properties;
但是这里的这个msgHandler只是Spring框架管理的一个bean,并没有被别的bean或者对象引用,那Spring的BeanFactory是怎么知道要从这个bean中获取配置信息呢?

查看PropertyPlaceholderConfigurer类的层级结构可以看到,这个类间接继承了 BeanFactoryPostProcessor接口,并且实现了 postProcessBeanFactory()方法:

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
	try {
		// 得到配置
		Properties mergedProps = mergeProperties();
		// 将得到的配置转换为合适的类型
		// Convert the merged properties, if necessary.
		convertProperties(mergedProps);
		// 将配置的内容告知BeanFactory
		// Let the subclass process the properties.
		processProperties(beanFactory, mergedProps);
	}
	catch (IOException ex) {
		throw new BeanInitializationException("Could not load properties", ex);
	}
}

当Spring加载任何实现了BeanFactoryPostProcessor接口的bean的配置时,都会在BeanFactory载入所有bean的配置之后执行 postProcessBeanFactory() 方法;

所以这里会调用重写的方法,并且先后执行 mergeProperties()、convertProperties()、processProperties()方法将配置内容告知BeanFactory。

所以正是通过实现BeanFactoryPostProcessor接口,BeanFactory会在实例化任何bean之前获得配置信息,从而能够正确解析bean定义中的变量引用。

2.4.1.2 激活(调用) BeanFactoryPostProcessor
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
	// 激活 BeanFactoryPostProcessors
	PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
	// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
	// (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
	if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
		beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
		beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
	}
}

PostProcessorRegistrationDelegate.java

public static void invokeBeanFactoryPostProcessors(
		ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
	// Invoke BeanDefinitionRegistryPostProcessors first, if any.
	Set<String> processedBeans = new HashSet<>();
	// 对BeanDefinitionRegistry类型的处理
	if (beanFactory instanceof BeanDefinitionRegistry) {
		BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
		// 记录硬编码方式注册的 BeanFactoryPostProcessor 类型的处理器
		List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
		// 记录硬编码方式注册的 BeanDefinitionRegistryPostProcessor 类型的处理器
		List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
		// 硬编码注册的后处理器
		for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
			if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
				BeanDefinitionRegistryPostProcessor registryProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor;
				// 对于 BeanDefinitionRegistryPostProcessor类型,在 BeanFactoryPostProcessor的基础上还有自己定义的方法,需要先调用
				// (激活 postProcessBeanDefinitionRegistry方法)
				registryProcessor.postProcessBeanDefinitionRegistry(registry);
				// 记录 BeanDefinitionRegistryPostProcessor
				registryProcessors.add(registryProcessor);
			}
			else {
				// 记录常规的 BeanFactoryPostProcessor
				regularPostProcessors.add(postProcessor);
			}
		}
		// Do not initialize FactoryBeans here: We need to leave all regular beans
		// uninitialized to let the bean factory post-processors apply to them!
		// Separate between BeanDefinitionRegistryPostProcessors that implement
		// PriorityOrdered, Ordered, and the rest.
		List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
		// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
		String[] postProcessorNames =
				beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
		for (String ppName : postProcessorNames) {
			if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
				currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
				processedBeans.add(ppName);
			}
		}
		// 排序
		sortPostProcessors(currentRegistryProcessors, beanFactory);
		registryProcessors.addAll(currentRegistryProcessors);
		// 激活 postProcessBeanDefinitionRegistry 方法
		invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
		currentRegistryProcessors.clear();
		// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
		// 对于配置中读取的 BeanDefinitionRegistryPostProcessor 的处理
		postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
		for (String ppName : postProcessorNames) {
			if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
				currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
				processedBeans.add(ppName);
			}
		}
		// 排序
		sortPostProcessors(currentRegistryProcessors, beanFactory);
		registryProcessors.addAll(currentRegistryProcessors);
		// 激活 postProcessBeanDefinitionRegistry 方法
		invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
		currentRegistryProcessors.clear();
		// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
		boolean reiterate = true;
		while (reiterate) {
			reiterate = false;
			// 对于配置中读取的 BeanDefinitionRegistryPostProcessor 的处理
			postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
			for (String ppName : postProcessorNames) {
				if (!processedBeans.contains(ppName)) {
					currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
					processedBeans.add(ppName);
					reiterate = true;
				}
			}
			// 排序
			sortPostProcessors(currentRegistryProcessors, beanFactory);
			registryProcessors.addAll(currentRegistryProcessors);
			// 激活 postProcessBeanDefinitionRegistry 方法
			invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
			currentRegistryProcessors.clear();
		}
		// Now, invoke the postProcessBeanFactory callback of all processors handled so far.
		// 激活 postProcessBeanFactory方法;(不需要排序)
		invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
		// 常规 BeanFactoryPostProcessor,也是激活 postProcessBeanFactory方法;(不需要排序)
		invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
	}
	else {
		// Invoke factory processors registered with the context instance.
		// 激活 postProcessBeanFactory方法
		invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
	}
	// Do not initialize FactoryBeans here: We need to leave all regular beans
	// uninitialized to let the bean factory post-processors apply to them!
	// 对于配置中读取的 BeanFactoryPostProcessor的处理
	String[] postProcessorNames =
			beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
	// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
	// Ordered, and the rest.
	List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
	List<String> orderedPostProcessorNames = new ArrayList<>();
	List<String> nonOrderedPostProcessorNames = new ArrayList<>();
	// 对后处理器进行分类
	for (String ppName : postProcessorNames) {
		if (processedBeans.contains(ppName)) {
			// skip - already processed in first phase above
			// 已经处理过的直接跳过
		}
		else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
			// 标注了 优先级order属性的处理器
			priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
		}
		else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
			// 标注了 order属性的处理器
			orderedPostProcessorNames.add(ppName);
		}
		else {
			// 没有标注 order属性才处理器
			nonOrderedPostProcessorNames.add(ppName);
		}
	}
	// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
	// 按照优先级进行排序
	sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
	// 激活 postProcessBeanFactory方法
	invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
	// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
	List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
	for (String postProcessorName : orderedPostProcessorNames) {
		orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
	}
	// 按照优先级进行排序
	sortPostProcessors(orderedPostProcessors, beanFactory);
	// 激活 postProcessBeanFactory方法
	invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
	// Finally, invoke all other BeanFactoryPostProcessors.
	List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
	for (String postProcessorName : nonOrderedPostProcessorNames) {
		nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
	}
	// 没有优先级,不用排序
	// 激活 postProcessBeanFactory方法
	invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
	// Clear cached merged bean definitions since the post-processors might have
	// modified the original metadata, e.g. replacing placeholders in values...
	beanFactory.clearMetadataCache();
}

上面的方法中可以看到,对于BeanFactoryPostProcessor的处理主要分为两种情况进行:一个是对于 BeanDefinitionRegistry类的特殊处理;另一种是对普通的BeanFactoryPostProcessor的处理;而对于每种情况都需要考虑硬编码注入注册的后处理器以及通过配置注入的后处理器。(这里实在太复杂了,而且我们学习源码暂时不用掌握这么细…也就没有深究@_@)

2.4.2 注册BeanPostProcessor

上面2.4.1小节提到了激活(调用) BeanFactoryPostProcessors,这一小节是注册 BeanPostProcessor,注册之后真正的调用其实是在bean的实例化阶段进行的

这是一个很重要的功能,也是BeanFactory不支持很多功能的原因;因为Spring中大部分功能都是通过后处理器的方式进行扩展的,但是BeanFactory中其实并没有实现后处理器的自定义注册,所以在调用的时候如果没有进行手动注册的话是不能使用的

但是在ApplicationContext中添加了自动注册的功能,所以在使用ApplicationContext容器的时候能够实现在bean实例化时对bean的属性的修改。

示例:

  1. 定义一个自定义BeanPostProcessor并实现 InstantiationAwareBeanPostProcessor
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if (bean instanceof User) {
			((User)bean).setName(((User)bean).getName() + 1);
			((User)bean).setPassword(((User)bean).getPassword() + 1);
		}
		return null;
	}
}

在这个 BeanPostProcessor中实现,对于User类型的bean的属性都+1;
2. 定义XML文件:

bean-post-processor-context.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="org.bgy.spring.study.spring.bean.definition.bean.post.processor.MyInstantiationAwareBeanPostProcessor"/>

    <bean id="user" class="org.bgy.spring.study.spring.bean.definition.bean.factory.post.processor.User">
        <property name="name" value="bgy"/>
        <property name="password" value="password"/>
    bean>
beans>
  1. 定义测试类:
public class MyInstantiationAwareBeanPostProcessorTest {
	public static void main(String[] args) {
		BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("bean-post-processor-context.xml"));
		User user = beanFactory.getBean("user", User.class);
		System.out.println(user);

		System.out.println("----------------------");

		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:bean-post-processor-context.xml");
		User user1 = applicationContext.getBean("user", User.class);
		System.out.println(user1);
	}
}

// output:
User{name='bgy', password='password'}
----------------------
User{name='bgy1', password='password1'}

可以看到,使用BeanFactory容器获取的bean,属性值没有变化;而使用ApplicationContext容器获取的bean,属性值均被+1了,也印证了这句话。

回到代码:

protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
	PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}

PostProcessorRegistrationDelegate.java

public static void registerBeanPostProcessors(
		ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
	String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
	// Register BeanPostProcessorChecker that logs an info message when
	// a bean is created during BeanPostProcessor instantiation, i.e. when
	// a bean is not eligible for getting processed by all BeanPostProcessors.
	// BeanPostProcessorChecker 是一个普通的信息打印,可能会有些情况:
	// 当Spring的配置中的后处理器还没有被注册就已经可以了bean的初始化时,就会打印出 BeanPostProcessorChecker 中设定的信息;
	int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
	beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
	// Separate between BeanPostProcessors that implement PriorityOrdered,
	// Ordered, and the rest.
	// 使用 PriorityOrdered 保证顺序
	List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
	List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
	// 使用 Ordered 保证顺序
	List<String> orderedPostProcessorNames = new ArrayList<>();
	// 无序 BeanPostProcessor
	List<String> nonOrderedPostProcessorNames = new ArrayList<>();
	for (String ppName : postProcessorNames) {
		if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
			BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
			priorityOrderedPostProcessors.add(pp);
			if (pp instanceof MergedBeanDefinitionPostProcessor) {
				internalPostProcessors.add(pp);
			}
		}
		else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
			orderedPostProcessorNames.add(ppName);
		}
		else {
			nonOrderedPostProcessorNames.add(ppName);
		}
	}
	// First, register the BeanPostProcessors that implement PriorityOrdered.
	// 第一步,注册所有实现了 PriorityOrdered 的 BeanPostProcessor
	// 排序
	sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
	// 注册
	registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);
	// Next, register the BeanPostProcessors that implement Ordered.
	List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
	for (String ppName : orderedPostProcessorNames) {
		BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
		orderedPostProcessors.add(pp);
		if (pp instanceof MergedBeanDefinitionPostProcessor) {
			internalPostProcessors.add(pp);
		}
	}
	// 第二步,注册所有实现了 Ordered 的 BeanPostProcessor
	// 排序
	sortPostProcessors(orderedPostProcessors, beanFactory);
	// 注册
	registerBeanPostProcessors(beanFactory, orderedPostProcessors);
	// Now, register all regular BeanPostProcessors.
	List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
	for (String ppName : nonOrderedPostProcessorNames) {
		BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
		nonOrderedPostProcessors.add(pp);
		if (pp instanceof MergedBeanDefinitionPostProcessor) {
			internalPostProcessors.add(pp);
		}
	}
	// 第三步,注册所有无序的 BeanPostProcessor(无序不用排序)
	registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
	// Finally, re-register all internal BeanPostProcessors.
	// 第四步,注册所有 MergedBeanDefinitionPostProcessor 类型的BeanPostProcessor;并非重复注册(在registerBeanPostProcessors会保证唯一性)
	sortPostProcessors(internalPostProcessors, beanFactory);
	// 在这里没有进行去重的原因是为了保持分类的效果,使逻辑更加清晰(猜测)
	registerBeanPostProcessors(beanFactory, internalPostProcessors);
	// Re-register post-processor for detecting inner beans as ApplicationListeners,
	// moving it to the end of the processor chain (for picking up proxies etc).
	beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

看起来对于BeanPostProcessor的处理和BeanFactoryPostProcessor的处理极为相似,我们也不去探究详细的实现了…

2.4.3 初始化消息资源

这个功能是用于支持i18n国际化问题的,具体的使用可以自行了解。

protected void initMessageSource() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
		// 如果在配置中已经配置了 messageSource,那么将 messageSource提取并记录
		this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
		// Make MessageSource aware of parent MessageSource.
		if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
			HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
			if (hms.getParentMessageSource() == null) {
				// Only set parent context as parent MessageSource if no parent MessageSource
				// registered already.
				hms.setParentMessageSource(getInternalParentMessageSource());
			}
		}
		if (logger.isTraceEnabled()) {
			logger.trace("Using MessageSource [" + this.messageSource + "]");
		}
	}
	else {
		// 如果用户并没有定义配置文件,那么使用临时的 DelegatingMessageSource以便作为调用 getMessage 方法的返回
		// Use empty MessageSource to be able to accept getMessage calls.
		DelegatingMessageSource dms = new DelegatingMessageSource();
		dms.setParentMessageSource(getInternalParentMessageSource());
		this.messageSource = dms;
		beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
		if (logger.isTraceEnabled()) {
			logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]");
		}
	}
}

这个方法的功能主要是提取配置中定义的messageSource(自定义的bean,里面会指定使用的国际化资源文件),并将其记录在Spring容器(AbstractApplicationContext)中;如果用户未设置资源文件的话,Spring会提供默认的配置DelegatingMessageSource。

在这个方法中获取messageSource的方式为:beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class); 即在这里Spring使用了硬编码的方式硬性规定了自定义资源文件的beanName必须为 MESSAGE_SOURCE_BEAN_NAME = “messageSource”;所以当在XML中进行配置时,这个bean的id一定要为messageSource。

通过读取并将自定义资源文件配置记录在容器中,那么就可以在获取资源文件的时候直接使用了;例如,在AbstractApplicationContext中的获取资源文件属性的方法为:

public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
	return getMessageSource().getMessage(resolvable, locale);
}

这里面的getMessageSource()方式就是获取之前记录的自定义资源配置。

2.4.4 初始化 ApplicationEventMulticaster

在看Spring的事件广播器之前,还是先来看一下Spring中事件监听器的使用:

  1. 定义监听事件
public class TestEvent extends ApplicationEvent {
	private String msg;

	public TestEvent(Object source) {
		super(source);
	}

	public TestEvent(Object source, String msg) {
		super(source);
		this.msg = msg;
	}

	public void print() {
		System.out.println(msg);
	}
}
  1. 定义监听器
public class TestListener implements ApplicationListener {
	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if(event instanceof TestEvent){
			TestEvent testEvent = (TestEvent)event;
			testEvent.print();
		}
	}
}
  1. 添加配置文件:
<bean id="testListener" class="org.bgy.spring.study.spring.bean.definition.event.listener.TestListener"/>
  1. 定义测试类:
public class EventListenerDemo {
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:event-listener-context.xml");

		TestEvent testEvent = new TestEvent("hello", "java");
		applicationContext.publishEvent(testEvent);
	}
}

// output:
java

程序运行时,我们自定义的监听器TestListener会监听到Spring发出的事件TestEvent,并进行进一步的处理(执行print())。

回到代码:

protected void initApplicationEventMulticaster() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	// 如果用户自定义了事件广播器,就使用用户自定义的事件广播器(存放监听器,并在合适的地方调用监听器)
	if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
		this.applicationEventMulticaster =
				beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
		if (logger.isTraceEnabled()) {
			logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
		}
	}
	else {
		// 如果用户没有自定义,就使用默认的 SimpleApplicationEventMulticaster 广播器
		this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
		beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
		if (logger.isTraceEnabled()) {
			logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
					"[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
		}
	}
}

代码逻辑比较简单:

  • 如果用户自定义了事件广播器,就使用用户自定义的事件广播器;
  • 如果用户没有自定义时间广播器,就使用默认的 SimpleApplicationEventMulticaster 广播器

按照前面的示例,我们可以推测:
作为事件广播器,需要存放事件监听器;并且在事件发布的时候,调用事件监听器。

查看一下事件发布的源码来验证一下推测:

protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
	Assert.notNull(event, "Event must not be null");
	// Decorate event as an ApplicationEvent if necessary
	ApplicationEvent applicationEvent;
	if (event instanceof ApplicationEvent) {
		applicationEvent = (ApplicationEvent) event;
	}
	else {
		applicationEvent = new PayloadApplicationEvent<>(this, event);
		if (eventType == null) {
			eventType = ((PayloadApplicationEvent<?>) applicationEvent).getResolvableType();
		}
	}
	// Multicast right now if possible - or lazily once the multicaster is initialized
	if (this.earlyApplicationEvents != null) {
		this.earlyApplicationEvents.add(applicationEvent);
	}
	else {
		getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
	}
	// Publish event via parent context as well...
	if (this.parent != null) {
		if (this.parent instanceof AbstractApplicationContext) {
			((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
		}
		else {
			this.parent.publishEvent(event);
		}
	}
}

可以看到这里调用了事件广播器的multicastEvent()方法来广播事件;
我们再进入默认的事件广播器 SimpleApplicationEventMulticaster查看multicastEvent()方法:

public void multicastEvent(ApplicationEvent event) {
	multicastEvent(event, resolveDefaultEventType(event));
}

public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
	ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
	Executor executor = getTaskExecutor();
	for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
		if (executor != null) {
			executor.execute(() -> invokeListener(listener, event));
		}
		else {
			invokeListener(listener, event);
		}
	}
}

这里可以看到,当发布Spring事件的时候,是遍历所有的监听器,并且对每个监听器执行 invokeListener()方法;

protected void invokeListener(ApplicationListener<?> listener, ApplicationEvent event) {
	ErrorHandler errorHandler = getErrorHandler();
	if (errorHandler != null) {
		try {
			doInvokeListener(listener, event);
		}
		catch (Throwable err) {
			errorHandler.handleError(err);
		}
	}
	else {
		doInvokeListener(listener, event);
	}
}

private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
	try {
		listener.onApplicationEvent(event);
	}
	catch (ClassCastException ex) {
		String msg = ex.getMessage();
		if (msg == null || matchesClassCastMessage(msg, event.getClass())) {
			// Possibly a lambda-defined listener which we could not resolve the generic event type for
			// -> let's suppress the exception and just log a debug message.
			Log logger = LogFactory.getLog(getClass());
			if (logger.isTraceEnabled()) {
				logger.trace("Non-matching event type for listener: " + listener, ex);
			}
		}
		else {
			throw ex;
		}
	}
}

再在方法里面去调用listener的onApplicationEvent()方法来传递事件,并由监听器决定是否处理事件;比如我们自定义的监听器中就只处理TestEvent类型的事件。

2.4.5 注册监听器到广播器中

protected void registerListeners() {
	// Register statically specified listeners first.
	// 硬编码方式注册的监听器的处理
	for (ApplicationListener<?> listener : getApplicationListeners()) {
		getApplicationEventMulticaster().addApplicationListener(listener);
	}
	// Do not initialize FactoryBeans here: We need to leave all regular beans
	// uninitialized to let post-processors apply to them!
	// 配置文件注册的监听器的处理
	String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
	for (String listenerBeanName : listenerBeanNames) {
		getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
	}
	// Publish early application events now that we finally have a multicaster...
	Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
	this.earlyApplicationEvents = null;
	if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
		for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
			// 发布事件到广播器中
			getApplicationEventMulticaster().multicastEvent(earlyEvent);
		}
	}
}

也就是把这里在配置文件中配置的监听器的bean注册到广播器中,以实现在这里通过getApplicationListeners()方法从广播器中获取监听器。

2.5 完成BeanFactory的剩余初始化工作

protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
	/**
	 * 
	 * 		
	 * 		 	
	 * 		 	  // conversionService的设置,自己实现Spring中的类型转换
	// Initialize conversion service for this context.
	if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
			beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
		beanFactory.setConversionService(
				beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
	}
	// Register a default embedded value resolver if no bean post-processor
	// (such as a PropertyPlaceholderConfigurer bean) registered any before:
	// at this point, primarily for resolution in annotation attribute values.
	if (!beanFactory.hasEmbeddedValueResolver()) {
		beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
	}
	// 尽早初始化LoadTimeWeaverAware bean,以便尽早注册它们的转换器
	// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
	String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
	for (String weaverAwareName : weaverAwareNames) {
		getBean(weaverAwareName);
	}
	// Stop using the temporary ClassLoader for type matching.
	beanFactory.setTempClassLoader(null);
	// 冻结所有bean定义,说明注册的bean定义将不被修改或者进行任何进一步的处理
	// Allow for caching all bean definition metadata, not expecting further changes.
	beanFactory.freezeConfiguration();
	// 初始化非延迟加载
	// Instantiate all remaining (non-lazy-init) singletons.
	beanFactory.preInstantiateSingletons();
}

2.5.1 ConversionService的设置

前面我们提到过使用自定义类型转换器来实现从String转换为Date的方式,除了这个Spring还提供了另外一种方式,就是使用Convert。
这里就是初始化ConversionService的配置。

2.5.2 冻结BeanDefinition

冻结所有的bean定义,说明注册的BeanDefinition将不被修改或者进行任何进一步的处理:

beanFactory.freezeConfiguration();

public void freezeConfiguration() {
	this.configurationFrozen = true;
	this.frozenBeanDefinitionNames = StringUtils.toStringArray(this.beanDefinitionNames);
}

2.5.3 初始化非延迟加载单例bean

ApplicationContext实现的默认行为就是在启动的时候将所有单例bean提前进行实例化;提前实例化的意思就是:所有的单例bean都会在ApplicationContext容器初始化过程中被创建并配置(参考BeanFactory容器,是在使用到bean时才被实例化)。

这个提前实例化过程就是在这里实现的:

beanFactory.preInstantiateSingletons();

DefaultListableBeanFactory.java

public void preInstantiateSingletons() throws BeansException {
	if (logger.isTraceEnabled()) {
		logger.trace("Pre-instantiating singletons in " + this);
	}
	// Iterate over a copy to allow for init methods which in turn register new bean definitions.
	// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
	List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
	// Trigger initialization of all non-lazy singleton beans...
	for (String beanName : beanNames) {
		RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
		if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
			if (isFactoryBean(beanName)) {
				Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
				if (bean instanceof FactoryBean) {
					FactoryBean<?> factory = (FactoryBean<?>) bean;
					boolean isEagerInit;
					if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
						isEagerInit = AccessController.doPrivileged(
								(PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit,
								getAccessControlContext());
					}
					else {
						isEagerInit = (factory instanceof SmartFactoryBean &&
								((SmartFactoryBean<?>) factory).isEagerInit());
					}
					if (isEagerInit) {
						getBean(beanName);
					}
				}
			}
			else {
				getBean(beanName);
			}
		}
	}
	// Trigger post-initialization callback for all applicable beans...
	for (String beanName : beanNames) {
		Object singletonInstance = getSingleton(beanName);
		if (singletonInstance instanceof SmartInitializingSingleton) {
			SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
			if (System.getSecurityManager() != null) {
				AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
					smartSingleton.afterSingletonsInstantiated();
					return null;
				}, getAccessControlContext());
			}
			else {
				smartSingleton.afterSingletonsInstantiated();
			}
		}
	}
}

if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) ,先去判断了bean是单例,并且不是延迟初始化的才会提前初始化。

2.6 finishRefresh

protected void finishRefresh() {
	// Clear context-level resource caches (such as ASM metadata from scanning).
	clearResourceCaches();
	// Initialize lifecycle processor for this context.
	// 初始化 LifecycleProcessor
	initLifecycleProcessor();
	// 启动所有实现了 Lifecycle 接口的bean
	// Propagate refresh to lifecycle processor first.
	getLifecycleProcessor().onRefresh();
	// 通过Spring的事件发布机制来发出 ContextRefreshedEvent事件,以保证对应的监听器可以做进一步的逻辑处理;
	// Publish the final event.
	publishEvent(new ContextRefreshedEvent(this));
	// Participate in LiveBeansView MBean, if active.
	LiveBeansView.registerApplicationContext(this);
}

在Spring中还提供了Lifecycle接口,Lifecycle中包含了start/stop方法;实现此接口后Spring会保证在启动的时候调用start方法开始生命周期,在停止的时候调用stop方法来结束生命周期,通常用来配置后台程序,在启动后一直运行。这个finishRefresh()方法正是这一功能的实现。

2.6.1 initLifecycleProcessor

当ApplicationContext启动或停止时,它会通过LifecycleProcessor来与所有声明的bean的周期做状态更新,而在LifecycleProcessor使用前首先需要初始化:

protected void initLifecycleProcessor() {
	ConfigurableListableBeanFactory beanFactory = getBeanFactory();
	if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) {
		this.lifecycleProcessor =
				beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class);
		if (logger.isTraceEnabled()) {
			logger.trace("Using LifecycleProcessor [" + this.lifecycleProcessor + "]");
		}
	} else {
		DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor();
		defaultProcessor.setBeanFactory(beanFactory);
		this.lifecycleProcessor = defaultProcessor;
		beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);
		if (logger.isTraceEnabled()) {
			logger.trace("No '" + LIFECYCLE_PROCESSOR_BEAN_NAME + "' bean, using " +
					"[" + this.lifecycleProcessor.getClass().getSimpleName() + "]");
		}
	}
}

2.6.2 onRefresh

启动所有实现了Lifecycle接口的bean:

public void onRefresh() {
	startBeans(true);
	this.running = true;
}

private void startBeans(boolean autoStartupOnly) {
	Map<String, Lifecycle> lifecycleBeans = getLifecycleBeans();
	Map<Integer, LifecycleGroup> phases = new HashMap<>();
	lifecycleBeans.forEach((beanName, bean) -> {
		if (!autoStartupOnly || (bean instanceof SmartLifecycle && ((SmartLifecycle) bean).isAutoStartup())) {
			int phase = getPhase(bean);
			LifecycleGroup group = phases.get(phase);
			if (group == null) {
				group = new LifecycleGroup(phase, this.timeoutPerShutdownPhase, lifecycleBeans, autoStartupOnly);
				phases.put(phase, group);
			}
			group.add(beanName, bean);
		}
	});
	if (!phases.isEmpty()) {
		List<Integer> keys = new ArrayList<>(phases.keySet());
		Collections.sort(keys);
		for (Integer key : keys) {
			phases.get(key).start();
		}
	}
}

2.6.3 publishEvent

当完成ApplicationContext初始化的时候,要通过Spring中的事件发布机制来发出 ContextRefreshedEvent事件,保证对应的监听器可以做进一步的逻辑处理:

protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
	Assert.notNull(event, "Event must not be null");
	// Decorate event as an ApplicationEvent if necessary
	ApplicationEvent applicationEvent;
	if (event instanceof ApplicationEvent) {
		applicationEvent = (ApplicationEvent) event;
	}
	else {
		applicationEvent = new PayloadApplicationEvent<>(this, event);
		if (eventType == null) {
			eventType = ((PayloadApplicationEvent<?>) applicationEvent).getResolvableType();
		}
	}
	// Multicast right now if possible - or lazily once the multicaster is initialized
	if (this.earlyApplicationEvents != null) {
		this.earlyApplicationEvents.add(applicationEvent);
	}
	else {
		getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
	}
	// Publish event via parent context as well...
	if (this.parent != null) {
		if (this.parent instanceof AbstractApplicationContext) {
			((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
		}
		else {
			this.parent.publishEvent(event);
		}
	}
}

3. 整体流程思维导图

最后附上一个整体流程的思维导图:Spring ApplicationContext 容器扩展。

你可能感兴趣的:(Spring,Spring源码,spring,java,源码)