spring源码学习之容器的扩展(一)

时间:2023-03-09 08:39:11
spring源码学习之容器的扩展(一)

  在前面的章节,我们一直以BeanFactory接口以及它的默认实现XmlBeanFactory为例进行解析,但是,spring还提供了另一个接口ApplicationContext,用于扩展BeanFactory中现有的功能。ApplicationContext和BeanFactory两者都是用于加载bean的,但是相比之下,ApplicationContext提供了更多的扩展功能,也就是说,ApplicationContext包含BeanFactory的所有功能,通常建议比BeanFactory优先,除非在一些限制的场合,比如字节长度对内存有很大的影响时(Applet)。绝大多数"典型的",企业应用和系统,ApplicationContext就是你需要使用的

那么究竟ApplicationContext和BeanFactor多出了哪些功能呢?这就是需要进行探索的,首先看一下两个类在加载配置文件写法上的不同
(1)使用BeanFactory方式加载xml
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml"));
(2)使用ApplicationContext方式加载xml
ApplicationContext bf = new ClassPathXmlApplicationContext("beanFactoryTest.xml");

我们以ClassPathXmlApplicationContext 作为切入点开始对整体功能进行分析
org.springframework.context.support包下的ClassPathXmlApplicationContext类

 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);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
}

设置路径是必不可少的步骤,ClassPathXmlApplicationContext中可以将配置文件的路径以数组的方式传入,ClassPathXmlApplicationContext可以对数组进行解析和
加载,而对于解析及功能实现都是在refresh()方法中实现

一、设置配置路径
在ClassPathXmlApplicationContext中支持多个配置路径以数组的方式同时传入
org.springframework.context.support包下的AbstractRefreshableConfigApplicationContext类中的

 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方法中会搜寻匹配的系统变量并替换

二、扩展功能
设置了路径之后,便可以根据路径做配置文件的解析以及各种功能的实现了,可以说refresh方法中包含了ApplicationContext中提供的全部功能,而且此函数中
逻辑非常清晰明了,使我们很容易分析对应的层次及逻辑

org.springframework.context.support包下AbstractApplicationContext类中

 @Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 准备刷新新的上下文
prepareRefresh(); // Tell the subclass to refresh the internal bean factory.
// 初始化BeanFactory,进行XML文件读取
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context.
// 对BeanFactory进行各种功能填充
prepareBeanFactory(beanFactory); try {
// Allows post-processing of the bean factory in context subclasses.
// 子类覆盖方法,做额外的处理
postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context.
// 激活各种BeanFactory处理器
invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation.
// 注册拦截bean创建的bean处理器,这里只是注册,真正的调用是在getBean的时候
registerBeanPostProcessors(beanFactory); // Initialize message source for this context.
// 为上下文初始化Message源,即不同语言的消息体,即国际化处理
initMessageSource(); // Initialize event multicaster for this context.
// 初始化应用消息广播器,并放入"applicationEventMulticaster"bean中
initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses.
// 留给子类来初始化其他bean
onRefresh(); // Check for listener beans and register them.
// 在所有注册的bean中查找Listener bean,注册到消息广播器中
registerListeners(); // Instantiate all remaining (non-lazy-init) singletons.
// 初始化剩下的单实例(非惰性的)
finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event.
// 完成刷新过程,通知生命周期处理器lifecycleProcessor刷新过程,同时发出ContextRefreshEvent通知别人
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();
}
}
}

下面概括一下ClassPathXmlApplicationContext初始化步骤,并从中解释一下,它为我们提供的功能
(1)初始化前的准备工作,例如对系统属性或者是环境变量进行准备和验证
在某种情况下,项目的使用需要读取某些系统变量,而这个变量的设置很可能会影响这系统的正确性,那么ClassPathXmlApplicationContext为我们提供的这个准备函数
就显得非常重要,它可以在spring启动的时候提前对必需的变量进行存在性验证
(2)初始化bean,并进行XML读取
之前提到ClassPathXmlApplicationContext包含着BeanFactory所提供的一切特性,那么在这一步骤中将会复用BeanFactory中的配置文件读取解析及其功能,这一步之后
ClassPathXmlApplicationContext实际上就已经包含了BeanFactory所提供的功能,也就是说可以进行bean的提取等基础操作了
(3)对BeanFactory进行各种功能的填充
对@Qualifier与@Autowired应该比较熟悉,那么这两个注解正是在这一步骤中增加的支持
(4)子类覆盖方法做格外的处理
spring之所以强大,为世人所推崇,除了它功能上为大家提供了便利外,还有一方面是他的完美架构,开放式的架构让使用它的程序员很容易根据业务需要扩展已经
存在的功能,这种开放式的设计在spring中随处可见,例如在本例中就提供了一个空的函数的实现postProcessBeanFactory来方便程序员在业务上进一步扩展
(5)激活各种BeanFactory处理器
(6)注册拦截bean创建的bean处理器,这里只是注册,真正调用的是在getBean的时候
(7)为上下文初始化Message源,即对不同的消息体进行国际化处理
(8)初始化应用消息广播器,并放入"applicationEventMulticaster"bean 中
(9)留给子类来初始化其他的bean
(10)在所有的bean中查找listener bean,并注册到消息广播器中
(11)初始化剩下的单实例(非惰性的)
(12)完成刷新过程,通知生命周期处理器lifecycleProcessor刷新过程,同时发出ContextRefreshEvent通知别

三、环境准备
prepareRefresh方法主要是做些准备工作,例如对系统属性以及环境变量的初始化以及验证
org.springframework.context.support包下AbstractApplicationContext类中

 protected void prepareRefresh() {
// Switch to active.
this.startupDate = System.currentTimeMillis();
this.closed.set(false);
this.active.set(true); if (logger.isInfoEnabled()) {
logger.info("Refreshing " + this);
} // Initialize any placeholder property sources in the context environment.
// 留给子类覆盖
initPropertySources(); // Validate that all properties marked as required are resolvable:
// see ConfigurablePropertyResolver#setRequiredProperties
// 验证需要的属性文件是否都放入环境中
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<>();
}

虽然在主要的代码initPropertySources()和getEnvironment().validateRequiredProperties()没有什么逻辑代码,没有做任何处理,但是其实我们需要很好的
深入理解下,其实用好了,还是作用很大的
(1)initPropertySources()
initPropertySources正符合spring开放式结构设计,给用户最大扩展spring的能力,用户可以根据自身需要重写initPropertySources方法,并在方法中进行个性化
属性设置和处理
(2)validateRequiredProperties
则是对属性进行验证,如何验证呢?作者提供了一个简单的例子:
需求:工程运行过程中用到的某一个设置(例如VAR)是从系统环境变零中取得的,而如果用户没有在系统环境变量中配置这个参数,那么功能可能不会工作。
说下在spring中的解决方案:思路就是对源码进行扩展,自定义一个类继承自ClassPathXmlApplicationContext

 public class MyClassPathXmlApplicationContext extends ClassPathXmlApplicationContext{
public MyClassPathXmlApplicationContext(String... configLocations){
super(configLocations);
} protected void initPropertySources(){
getEnvironment().setRequiredProperties("VAR");
}
}

我们自定义了继承自ClassPathXmlApplicationContext的MyClassPathXmlApplicationContext,并重写了initPropertySources方法,在方法中添加了我们的个性化需求
那么在程序走到getEnvironment().validateRequiredProperties();代码的时候,如果系统没有检测到对应VAR 环境变量,那么抛出异常,当然我们使用的时候需要
将ClassPathXmlApplicationContext替换掉
ApplicationContext bf = new MyClassPathXmlApplicationContext("beanFactoryTest.xml");

四、加载BeanFactory
obtainFreshBeanFactory方法从字面理解是获取BeanFactory。之前有说过,ApplicationContext是对BeanFactory的扩展,不但包含了BeanFactory的全部功能,更在其
基础上做了大量的扩展应用,那么obtainFreshBeanFactory正是实现BeanFactory的地方,也就是经过了这个函数后ApplicationContext就拥有了BeanFactory的全部功能

 org.springframework.context.support包下AbstractApplicationContext类中
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
// 初始化BeanFactory,并进行XML文件读取,并将得到的BeanFactory记录到当前实体的属性中
refreshBeanFactory();
// 返回当前实体的beanFactory属性
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
}
return beanFactory;
}

具体的实现逻辑是在refreshBeanFactory方法中
org.springframework.context.support包下的AbstractRefreshableApplicationContext类中:

 @Override
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
// 创建DefaultListableBeanFactory
DefaultListableBeanFactory beanFactory = createBeanFactory();
// 为了序列化指定id,如果需要的话,让这个BeanFactory从id反序列化到BeanFactory对象
beanFactory.setSerializationId(getId());
// 定制beanFactory,设置相关属性,包括是否允许覆盖同名称的不同定义的对象以及循环依赖以及设置
// @Autowired @Qualifier注解解析器QualifierAnnotationAutowireCandidateResolver
customizeBeanFactory(beanFactory);
// 初始化DodumentReader,并进行XML读取和解析
loadBeanDefinitions(beanFactory);
synchronized (this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}

详细分析上面的那几个步骤:
(1)创建DefaultListableBeanFactory
在介绍BeanFactory的时候,声明方式为:BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml"));
其中XmlBeanFactory继承自DefaultListableBeanFactory,并提供了XmlBeanDefinitionReader类型的reader属性,也就是说DefaultListableBeanFactory是容器的
基础,必须首先要实例化,那么在这里就是实例化DefaultListableBeanFactory的步骤
(2)指定序列化ID
(3)定制BeanFactory
(4)加载BeanDefinition
(5)使用全局变量记录BeanFactory类实例
因为DefaultListableBeanFactory类型的变量beanFactory是函数内的局部变量,所以要使用全局变量记录解析结果

4.1 定制BeanFactory
这里已经开始了对BeanFactory的扩展,在基本容器的基础上,增加了是否允许覆盖是否允许扩展的设置并提供了注解@Autowired和@Qualifier支持
org.springframework.context.support包下的AbstractRefreshableApplicationContext类中:

 protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
// 如果allowBeanDefinitionOverriding不为空,设置beanFactory相关属性,此属性的含义是允许覆盖同名称的不同定义的对象
if (this.allowBeanDefinitionOverriding != null) {
beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
// allowCircularReferences不为空,设置beanFactory相关属性,此属性的含义是否允许bean之间存在循环依赖
if (this.allowCircularReferences != null) {
beanFactory.setAllowCircularReferences(this.allowCircularReferences);
}
}

对于允许覆盖和允许依赖这里只是判断了是否为空,如果不为空,则需要设置,但是并没有看到在哪里设置,具体实现是使用子类覆盖:

 public class MyClassPathXmlApplicationContext extends ClassPathXmlApplicationContext{
。。。
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) { beanFactory.setAllowBeanDefinitionOverriding(false);
beanFactory.setAllowCircularReferences(false);
super.customizeBeanFactory(beanFactory);
}
}

对于定制BeanFactory,spring中还提供了另外一个重要的扩展,就是设置AutowireCandidateResolver,在bean加载部分中讲解创建bean时,如果采用autowireType
方式注入,那么默认会使用spring提供的SimpleAutowireCandidateResolver,而对于默认的实现并没有过多的逻辑处理,在这里spring使用了QualifierAnnotationAutowireCandidateResolver
设置这个解析器后,spring就可以支持注解的方式注入了。
在讲解根据类型自定注入的时候,我们说过解析autowire类型时首先会调用方法:
Object value = getAutowireCandidateResolver().getSuggestedValue(descriptor);
因此我们知道在QualifierAnnotationAutowireCandidateResolver中一定会提供解析Qualifier和Autowired注解的方法

org.springframework.beans.factory.annotation包下QualifierAnnotationAutowireCandidateResolver类中:

 @Override
@Nullable
public Object getSuggestedValue(DependencyDescriptor descriptor) {
Object value = findValue(descriptor.getAnnotations());
if (value == null) {
MethodParameter methodParam = descriptor.getMethodParameter();
if (methodParam != null) {
value = findValue(methodParam.getMethodAnnotations());
}
}
return value;
}

4.2 加载BeanDefinition
XmlBeanDefinitionReader的初始化以及读取XML文件
org.springframework.context.support包下AbstractXmlApplicationContext类中

 @Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
// 为指定的BeanFactory创建XmlBeanDefinitionReader
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);
}
// 与上一个方法在同一个类中
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);
}
}

XmlBeanDefinitionReader已经将之前的初始化的DefaultListableBeanFactory注册进去了,所以XmlBeanDefinitionReader所读取的BeanDefinitionHolder都会注册
到DefaultListableBeanFactory,也就是经过这个步骤之后,DefaultListableBeanFactory类型的beanFactory已经包含了所有解析好的配置

五、功能扩展
进入函数prepareBeanFactory前,spring已经完成了对配置的解析,而ApplicationContext在功能上的扩展也由此展开
org.springframework.context.support包下的AbstractApplicationContext类中

 protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// Tell the internal bean factory to use the context's class loader etc.
// 设置beanFactory的classLoader为当前context的classLoader
beanFactory.setBeanClassLoader(getClassLoader());
// 设置beanFactory的表达式语言处理器,spring3增加了表达式语言的支持
// 默认使用#{bean.xxx}的形式来调用相关属性值
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
// 为beanFactory增加了一个默认的propertyEditor,这个主要是对bean属性等设置管理的一个工具
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment())); // Configure the bean factory with context callbacks.
// 添加BeanPostProcessor
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
// 设置几个忽略自动装配的接口
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.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.
// 增加对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)将相关环境变量及属性注册以单例模式注册

5.1 增加SpEL语言支持
spring表达式语言全称是 spring Expression Language,缩写为SpEL,能在运行时构建复杂表达式,存取对象图属性、对象方法调用等,并且能与spring功能
完美结合,比如能用来配置bean的定义,SpEL是单独模块,值依赖core模块,不依赖其他模块,可以单独使用
SpEL使用#{...},作为界定符,所有在大括号中的字符都将被认为是SpEL,使用格式如下:

 <bean id="saxophone" value="com.xxx.xxx.xxx" />
<bean>
<property name="instrument" value="#{saxophone}"></property>
</bean>
// 相当于:
<bean id="saxophone" value="com.xxx.xxx.xxx" />
<bean>
<property name="instrument" ref="saxophone"></property>
</bean>

当然上面只是列举的简单情况,SpEL功能是非常强大的,使用好,可大大提高开发效率
在源码中通过代码beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));注册语言解析器
就可以对SpEL语言进行解析了,但是注册解析器后spring又是在什么时候调用这个解析器进行解析的呢?
之前我们讲解过spring在bean进行初始化的时候会有属性填充的这一步,而在这一步中spring会调用AbstractAutowireCapableBeanFactory类的applyPropertyValues函数去
完成这一功能,就在这个函数中,会通过构造函数BeanDefinitionValueResolver类型的valueResolver来进行属性值的解析,同时,也是在这个步骤中一般通过
AbstractBeanFactory类中的evaluateBeanDefinitionString去完成SpEL解析

 @Nullable
protected Object evaluateBeanDefinitionString(@Nullable String value, @Nullable BeanDefinition beanDefinition) {
if (this.beanExpressionResolver == null) {
return value;
} Scope scope = null;
if (beanDefinition != null) {
String scopeName = beanDefinition.getScope();
if (scopeName != null) {
scope = getRegisteredScope(scopeName);
}
}
return this.beanExpressionResolver.evaluate(value, new BeanExpressionContext(this, scope));
}

当调用这个方法时候,会判断是否存在语言解析器,如果存在则调用语言解析器的方法进行解析,解析的过程是在spring的expression的包内,我们通过查看
evaluateBeanDefinitionString方法的调用层次可以看出,应用语言解释器的调用主要是在解析依赖注入bean的时候,以及在完成bean的初始化和属性获取后进行属性填充的时候

5.2 增加属性注册编辑器
在spring依赖注入的时候可以把普通属性注入进来,但是像Date类型就无法识别,例如:

public class UserManager {
private Date dateValue; public Date getDateValue(){
return dateValue;
} public void setDateValue(Date dateValue){
this.dateValue = dateValue;
} public String toString(){
return "dateValue:" + dateValue;
}
} // 上面代码中,需要对日期型属性进行注入:
<bean id="userManager" class="com.test.UserManager">
<property name="dateValue">
<value>2019-7-23</value>
</property>
</bean>
// 测试代码:
@Test
public void testDate(){
ApplicationContext = ctx = new ClassPathXmlApplicationContext("beans.xml");
UserManager userManager = (UserManager) ctx.getBean("userManager");
System.out.println(userManager);
}

如果直接这样使用,程序则会报错,类型转换不成功,因为带UserManager中的dateValue属性是Date类型的,而在XML中配置的却是String类型的,所以会报错!

spring中如何解决这种问题:两种方案

1、使用自定义属性编辑器
使用自定义属性编辑器,通过继承PropertyEditorSupport,这个类是在jdk中java.beans包下,重写setAsText方法
(1)编写自定义的属性编辑器

 public class DatePropertyEditor extends PropertyEditorSupport{

     private String format = "yyyy-MM-dd";

     public void setFormat(String format){
this.format = format;
} public void setAsText(String arg0) throws IllegalStateException {
System.out.println("arg0:" + arg0);
SimpleDateFormat sdf = new SimpleDateFormat(format);
try{
Date d = sdf.parse(arg0);
this.setFormat(d);
}catch(Exception e){
e.printStakeTrace();
}
}
}

(2)将自定义属性编辑器注册到spring中

 <!--自定义属性编辑器-->
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="java.util,Date" />
<bean class="com.test.DatePropertyEditor">
<property name="format" value="yyyy-MM-dd"/>
</bean>
</map>
</property>
</bean>

在配置文件中引入类型为org.springframework.beans.factory.config.CustomEditorConfigurer的bean,并在属性customEditors中加入自定义的属性编辑器,其中
key 为属性编辑器所对应的类型。通过这样的配置,当spring在注入bean的属性时一旦遇到了java.util.Date类型的属性会自动调用自定义的DatePropertyEditor
解析器进行解析,并用解析结果代替配置属性进行注入

2、注册spring自带的属性编辑器CustomDateEditor
具体步骤如下:

 (1)定义属性编辑器
public class DatePropertyEditorRegistrar implements PropertyEditorRegistrar{
public void registerCustomEditors(PropertyEditorRegistry registry){
registry.registerCustomEditors(Date.Class,new CustomEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
}
(2)注册到spring中
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name=""propertyEditorRegistrars>
<list>
<bean class="com.test.DatePropertyEditorRegistrar"></bean>
</list>
</property>
</bean>

通过在配置文件中将自定义的DatePropertyEditorRegistrar注册进入org.springframework.beans.factory.config.CustomEditorConfigurer中

我们了解自定义编辑器额使用,但是似乎与本节的核心代码beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
并无关联,因为在注册自定义属性编辑器的时候使用的是PropertyEditorRegistrar的registerCustomEditors方法,而这里使用的是ConfigurableListableBeanFactory
的addPropertyEditorRegistrar方法,我们看ResourceEditorRegistrar类,在ResourceEditorRegistrar中,有一个最主要的registerCustomEditors方法,

org.springframework.beans.support包下的ResourceEditorRegistrar类

 @Override
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函数中,可以看到之前提到的自定义属性中使用的关键代码,registry.registerCustomEditor(requiredType, editor);回过头来看ResourceEditorRegistrar
类的registerCustomEditors方法的核心功能,其实无非是注册了一系列的常用类型的属性编辑器,例如,代码doRegisterEditor(registry, Class.class, baseEditor);
实现的功能就是注册Class类对应的属性编辑器,那么,注册后,一旦某个实体bean中,那么,注册后,一旦某个实体bean中存在一些Class属性,那么spring就会调用
ClassEditor将配置中定义的String类型转换为Class类型并赋值

分析到这里,我们不禁有个疑问,虽说ResourceEditorRegistrar类的registerCustomEditors方法实现了批量注册的功能,但是beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()))
仅仅是注册了ResourceEditorRegistrar实例,却并没有调用ResourceEditorRegistrar的registerCustomEditors方法进行注册,那么到底什么时候注册呢?

选中registerCustomEditors,Ctrl+Alt+H 查看一下该方法的调用层次,这里又学了一招,查看方法调用层次的快捷键,也可以右键open call hierarchy
查看结果,可以知道在AbstractBeanFactory类中被调用过,其中还有一个熟悉的initBeanWrapper方法,这是在bean初始化时候使用的一个方法,在将BeanDefinition装换为
BeanWapper后对属性进行填充,至此,bean的初始化后会调用ResourceEditorRegistrar类的registerCustomEditors方法进行批量的通用属性编辑器注册,注册后,
在属性填充的环节便可以直接让spring使用这些编辑器属性的解析了

既然提到了BeanWapper,这里说明一下,spring中用于封装的bean是BeanWapper类型,而它又间接实现了PropertyEditorRegistry接口,也就是我们之前反复看到的参数
PropertyEditorRegistry registry,其实大部分情况下都是BeanWapper,对于BeanWapper在spring中的实现是BeanWapperImpl,而BeanWapperImpl除了实现BeanWapper,
还继承了PropertyEditorRegistrySupport类,在PropertyEditorRegistrySupport中有一个这样的方法:

org.springframework.beans包下的PropertyEditorRegistrySupport类

 private void createDefaultEditors() {
this.defaultEditors = new HashMap<>(64); // Simple editors, without parameterization capabilities.
// The JDK does not contain a default editor for any of these target types.
this.defaultEditors.put(Charset.class, new CharsetEditor());
this.defaultEditors.put(Class.class, new ClassEditor());
this.defaultEditors.put(Class[].class, new ClassArrayEditor());
this.defaultEditors.put(Currency.class, new CurrencyEditor());
this.defaultEditors.put(File.class, new FileEditor());
this.defaultEditors.put(InputStream.class, new InputStreamEditor());
this.defaultEditors.put(InputSource.class, new InputSourceEditor());
this.defaultEditors.put(Locale.class, new LocaleEditor());
this.defaultEditors.put(Path.class, new PathEditor());
this.defaultEditors.put(Pattern.class, new PatternEditor());
this.defaultEditors.put(Properties.class, new PropertiesEditor());
this.defaultEditors.put(Reader.class, new ReaderEditor());
this.defaultEditors.put(Resource[].class, new ResourceArrayPropertyEditor());
this.defaultEditors.put(TimeZone.class, new TimeZoneEditor());
this.defaultEditors.put(URI.class, new URIEditor());
this.defaultEditors.put(URL.class, new URLEditor());
this.defaultEditors.put(UUID.class, new UUIDEditor());
this.defaultEditors.put(ZoneId.class, new ZoneIdEditor()); // Default instances of collection editors.
// Can be overridden by registering custom instances of those as custom editors.
this.defaultEditors.put(Collection.class, new CustomCollectionEditor(Collection.class));
this.defaultEditors.put(Set.class, new CustomCollectionEditor(Set.class));
this.defaultEditors.put(SortedSet.class, new CustomCollectionEditor(SortedSet.class));
this.defaultEditors.put(List.class, new CustomCollectionEditor(List.class));
this.defaultEditors.put(SortedMap.class, new CustomMapEditor(SortedMap.class)); // Default editors for primitive arrays.
this.defaultEditors.put(byte[].class, new ByteArrayPropertyEditor());
this.defaultEditors.put(char[].class, new CharArrayPropertyEditor()); // The JDK does not contain a default editor for char!
this.defaultEditors.put(char.class, new CharacterEditor(false));
this.defaultEditors.put(Character.class, new CharacterEditor(true)); // Spring's CustomBooleanEditor accepts more flag values than the JDK's default editor.
this.defaultEditors.put(boolean.class, new CustomBooleanEditor(false));
this.defaultEditors.put(Boolean.class, new CustomBooleanEditor(true)); // The JDK does not contain default editors for number wrapper types!
// Override JDK primitive number editors with our own CustomNumberEditor.
this.defaultEditors.put(byte.class, new CustomNumberEditor(Byte.class, false));
this.defaultEditors.put(Byte.class, new CustomNumberEditor(Byte.class, true));
this.defaultEditors.put(short.class, new CustomNumberEditor(Short.class, false));
this.defaultEditors.put(Short.class, new CustomNumberEditor(Short.class, true));
this.defaultEditors.put(int.class, new CustomNumberEditor(Integer.class, false));
this.defaultEditors.put(Integer.class, new CustomNumberEditor(Integer.class, true));
this.defaultEditors.put(long.class, new CustomNumberEditor(Long.class, false));
this.defaultEditors.put(Long.class, new CustomNumberEditor(Long.class, true));
this.defaultEditors.put(float.class, new CustomNumberEditor(Float.class, false));
this.defaultEditors.put(Float.class, new CustomNumberEditor(Float.class, true));
this.defaultEditors.put(double.class, new CustomNumberEditor(Double.class, false));
this.defaultEditors.put(Double.class, new CustomNumberEditor(Double.class, true));
this.defaultEditors.put(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true));
this.defaultEditors.put(BigInteger.class, new CustomNumberEditor(BigInteger.class, true)); // Only register config value editors if explicitly requested.
if (this.configValueEditorsActive) {
StringArrayPropertyEditor sae = new StringArrayPropertyEditor();
this.defaultEditors.put(String[].class, sae);
this.defaultEditors.put(short[].class, sae);
this.defaultEditors.put(int[].class, sae);
this.defaultEditors.put(long[].class, sae);
}
}

这里就是spring总定义的默认的属性编辑器,但是没有定义的哪些,就需要进行自定义设置了

5.3 添加ApplicationContextAwareProcessor处理器
了解了属性编辑器的使用后,接下来我们继续通过AbstractApplicationContext的prepareBeanFactory进行函数跟踪,对于beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
其实主要目的就是注册个BeanPostProcessor,而真正的逻辑是在ApplicationContextAwareProcessor中
ApplicationContextAwareProcessor实现BeanPostProcessor接口,对于这个类,我们也只关心postProcessBeforeInitialization和postProcessAfterInitialization这
两个方法

org.springframework.context.support包下的ApplicationContextAwareProcessor类中

 // 在postProcessAfterInitialization方法中没有做任何处理
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
return bean;
} // 重点看一下postProcessBeforeInitialization方法
@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<Object>) () -> {
invokeAwareInterfaces(bean);
return null;
}, acc);
}
else {
invokeAwareInterfaces(bean);
} return bean;
}
// 最终会调用invokeAwareInterfaces方法,这个在同一个类中
private void invokeAwareInterfaces(Object bean) {
if (bean instanceof Aware) {
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方法,实现这些Aware接口的bean在初始化之后,可以取得对应的一些资源

5.4 设置忽略依赖
当spring将ApplicationContextAwareProcessor注册后,那么在invokeAwareInterfaces方法中简介调用Aware类已经不是普通的bean了,那么需要spring在做bean
依赖的时候忽略他们,ignoreDependencyInterface方法正是这个作用
org.springframework.context.support包下的AbstractApplicationContext类中prepareBeanFactory方法中
// 设置几个忽略自动装配的接口
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

5.5 注册依赖
spring中有了忽略依赖的功能,当然必不可少会有注册依赖的功能

org.springframework.context.support包下的AbstractApplicationContext类中prepareBeanFactory方法中
// 设置了几个自动装备的特殊规则
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);

当注册依赖解析后,例如注册了对BeanFactory.class的解析依赖后,当bean的属性注入的时候,一旦检测到属性为BeanFactory类型便会将BeanFactory注入进去