private static ApplicationContext applicationContext;static {applicationContext = new ClassPathXmlApplicationContext("bean.xml");
}
BeanFactory的功能扩展类ApplicationContext进行深度的分析。ApplicationConext与 BeanFactory的功能相似,都是用于向IOC中加载Bean的。由于ApplicationConext的功能是多于BeanFactory的,所以在日常使用中,建议直接使用ApplicationConext即可。在 ApplicationConext的构造方法中,主要做了两个事情:
① 设置配置的加载路径;
② 执行ApplicationConext中,所有功能的初始化操作;
/*** 创建一个新的ClassPathXmlApplicationContext,从给定的XML文件中加载定义。*/
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {this(configLocations, refresh, null);
}/*** 使用给定的父类创建一个新的ClassPathXmlApplicationContext,从给定的XML文件中加载定义。*/
// eg1:configLocations=["bean.xml"],refresh=true,parent=null
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {super(parent);/** 1:设置配置路径 */setConfigLocations(configLocations); // eg1:configLocations=["bean.xml"]/** 2:执行初始化操作 */if (refresh) // eg1:truerefresh();
}
setConfigLocations 设置配置加载路径:
该方法逻辑不多,主要就是为应用上下文AbstractRefreshableConfigApplicationContext 类设置配置路径(String[] configLocations),源码如下所示:
/*** 为应用上下文(application context)设置配置路径(config locations)*/
// eg1:locations=["bean.xml"]
public void setConfigLocations(@Nullable String... locations) {if (locations != null) {Assert.noNullElements(locations, "Config locations must not be null");this.configLocations = new String[locations.length];/** 如果路径中包含特殊符号(如:${var}),那么在resolvePath方法中会搜寻匹配的系统变量并且进行替换操作 */for (int i = 0; i < locations.length; i++)this.configLocations[i] = resolvePath(locations[i]).trim(); // eg1:configLocations=["bean.xml"] // xml配置文件所在路径}elsethis.configLocations = null;
}
/*** 解析给定的路径,必要时用相应的环境属性值替换占位符。应用于配置位置。*/
// eg1:path="bean.xml"
protected String resolvePath(String path) {return getEnvironment().resolveRequiredPlaceholders(path);
}
// eg1:text="bean.xml"
@Override
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {// eg1:propertyResolver=AbstractPropertyResolverreturn this.propertyResolver.resolveRequiredPlaceholders(text);
}
// eg1:text="bean.xml"
@Override
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {if (this.strictHelper == null) // eg1:strictHelper=nullthis.strictHelper = createPlaceholderHelper(false);return doResolvePlaceholders(text, this.strictHelper);
}
/*** 创建PropertyPlaceholderHelper实例对象*/
private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) {/** placeholderPrefix = "${", placeholderSuffix = "}", valueSeparator = ":" */return new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix,this.valueSeparator, ignoreUnresolvablePlaceholders);
}// eg1:text="bean.xml", helper=PropertyPlaceholderHelper
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {return helper.replacePlaceholders(text, this::getPropertyAsRawString);
}
/*** 将格式${name}中的所有占位符替换为提供的PlaceholderResolver的返回值。*/
// eg1:value="bean.xml"
public String replacePlaceholders(String value, PlaceholderResolver placeholderResolver) {Assert.notNull(value, "'value' must not be null");return parseStringValue(value, placeholderResolver, null);
}// eg1:value="bean.xml",visitedPlaceholders=null
protected String parseStringValue(String value, PlaceholderResolver placeholderResolver, @Nullable Set<String> visitedPlaceholders) {int startIndex = value.indexOf(this.placeholderPrefix);// eg1:startIndex=-1if (startIndex == -1) return value; // eg1:return "bean.xml"StringBuilder result = new StringBuilder(value);while (startIndex != -1) {int endIndex = findPlaceholderEndIndex(result, startIndex);if (endIndex != -1) {String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);String originalPlaceholder = placeholder;if (visitedPlaceholders == null) {visitedPlaceholders = new HashSet<>(4);}if (!visitedPlaceholders.add(originalPlaceholder)) {throw new IllegalArgumentException("Circular placeholder reference '" + originalPlaceholder + "' in property definitions");}// Recursive invocation, parsing placeholders contained in the placeholder key.placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);// Now obtain the value for the fully resolved key...String propVal = placeholderResolver.resolvePlaceholder(placeholder);if (propVal == null && this.valueSeparator != null) {int separatorIndex = placeholder.indexOf(this.valueSeparator);if (separatorIndex != -1) {String actualPlaceholder = placeholder.substring(0, separatorIndex);String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder);if (propVal == null) {propVal = defaultValue;}}}if (propVal != null) {// Recursive invocation, parsing placeholders contained in the// previously resolved placeholder value.propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);if (logger.isTraceEnabled()) {logger.trace("Resolved placeholder '" + placeholder + "'");}startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length());}else if (this.ignoreUnresolvablePlaceholders) {// Proceed with unprocessed value.startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());}else {throw new IllegalArgumentException("Could not resolve placeholder '" +placeholder + "'" + " in value \"" + value + "\"");}visitedPlaceholders.remove(originalPlaceholder);}else {startIndex = -1;}}return result.toString();
}
refresh 初始化工作:
在refresh()方法中几乎包含了ApplicationContext中提供的全部功能,下面我们会针对这 个方法进行详细的分析:
/*** 返回静态指定的ApplicationListeners的列表*/
public Collection<ApplicationListener<?>> getApplicationListeners() {return this.applicationListeners;
}@Override
public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");/** 1:为refresh操作做提前的准备工作 */prepareRefresh();/** 2:获得beanFactory实例对象 */ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();/** 3:准备用于此上下文的beanFactory */prepareBeanFactory(beanFactory);try {postProcessBeanFactory(beanFactory); // 允许在上下文子类中对BeanFactory进行后置处理(空方法,可由子类实现)StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");/** 4:激活各种BeanFactoryPostProcessor的后置处理器 */invokeBeanFactoryPostProcessors(beanFactory);/** 5:注册各种Bean的后置处理器,在getBean时才会被调用 */registerBeanPostProcessors(beanFactory);beanPostProcess.end();/** 6:为上下文初始化消息源(即:国际化处理)*/initMessageSource();/** 7:为上下文初始化应用事件广播器 */initApplicationEventMulticaster();onRefresh(); // 初始化特定上下文子类中的其他特殊bean(空方法,可由子类实现)/** 8:在所有注册的bean中查找listener bean,并注册到消息广播器中 */registerListeners();/** 9:初始化剩下的单例(非惰性non-lazy-init)*/finishBeanFactoryInitialization(beanFactory);/** 10:完成refresh,通知lifecycleProcessor刷新过程,同时发出ContextRefreshEvent通知 */finishRefresh();} catch (BeansException ex) {……} finally {……}}
}
/** 1:为refresh操作做提前的准备工作 */
prepareRefresh主要是为了容器进行刷新做准备,它实现的步骤如下:
① 设置容器的启动时间。
② 设置活跃状态为true。
③ 设置关闭状态为false
④ 获取Environment对象,并加载当前系统的属性值到Environment对象中。
⑤ 准备监听器和事件的集合对象,默认为空的集合。
/*** 为刷新准备这个上下文,设置它的启动日期和活动标志,以及执行任何属性源的初始化。*/
protected void prepareRefresh() {this.startupDate = System.currentTimeMillis(); // 设置容器启动的时间this.closed.set(false); // 容器的关闭标志位this.active.set(true); // 容器的激活标志位initPropertySources(); // 留给子类覆盖,初始化属性资源(空方法)/** 创建并获取环境对象,验证需要的属性文件是否都已经放入环境中 */getEnvironment().validateRequiredProperties();// 如果为空,则以applicationListeners为准if (this.earlyApplicationListeners == null) // eg1:earlyApplicationListeners=nullthis.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners); // eg1:applicationListeners.size()=0// 如果不等于空,则清空applicationListeners,以earlyApplicationListeners为准else {this.applicationListeners.clear();this.applicationListeners.addAll(this.earlyApplicationListeners);}// 创建刷新前的监听事件集合this.earlyApplicationEvents = new LinkedHashSet<>();
}
validateRequiredProperties():
@Override
public void validateRequiredProperties() {MissingRequiredPropertiesException ex = new MissingRequiredPropertiesException();for (String key : this.requiredProperties) { // eg1: requiredProperties.size()=0if (this.getProperty(key) == null)ex.addMissingRequiredProperty(key);}if (!ex.getMissingRequiredProperties().isEmpty()) // eg1:ex.getMissingRequiredProperties().size()=0throw ex;
}
/** 2:obtainFreshBeanFactory 获得beanFactory实例对象 */
通过obtainFreshBeanFactory()这个方法,ApplicationContext就已经拥有了BeanFactory 的全部功能。而这个方法中也包含了前面我们介绍BeanFactory时候对于xml配置的加载 过程。
/*** 告诉子类刷新内部bean工厂。*/
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {/** 1:初始化BeanFactory,并加载bean的配置文件 */refreshBeanFactory(); // eg1:AbstractRefreshableApplicationContext#refreshBeanFactory()/** 2:获得BeanFactory */return getBeanFactory(); // eg1:AbstractRefreshableApplicationContext#getBeanFactory()
}
obtainFreshBeanFactory() 的 1:初始化BeanFactory,并加载bean的配置文件
/*** 此实现执行此上下文的底层bean工厂的实际刷新,关闭前一个bean工厂(如果有的话)并为上下文生命周期的下一阶段初始化一个新的bean工厂。*/
@Override
protected final void refreshBeanFactory() throws BeansException {/** 1: 如果存在BeanFactory,则执行清理操作(即:删除Bean及关闭BeanFactory) */if (hasBeanFactory()) { // eg1:falsedestroyBeans();closeBeanFactory();}try {/** 2:创建DefaultListableBeanFactory实例对象 */DefaultListableBeanFactory beanFactory = createBeanFactory();// eg1:(getId()="org.springframework.context.support.ClassPathXmlApplicationContext@2bec854f"beanFactory.setSerializationId(getId());/** 3:定制此上下文使用的内部bean工厂,向BeanFactory设置setAllowBeanDefinitionOverriding和setAllowCircularReferences */customizeBeanFactory(beanFactory);/** 4:加载BeanDefinition */loadBeanDefinitions(beanFactory); // eg1:AbstractXmlApplicationContext#loadBeanDefinitions(beanFactory)this.beanFactory = beanFactory;}catch (IOException ex) {throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);}
}
1: 如果存在BeanFactory,则执行清理操作(即:删除Bean及关闭BeanFactory)
/*** 确定此上下文当前是否持有bean工厂,即至少被刷新过一次且尚未关闭。*/
protected final boolean hasBeanFactory() {return (this.beanFactory != null); // 开始的时候beanFactory为空
}
2:创建DefaultListableBeanFactory实例对象:new一个
/*** 为此上下文创建一个内部bean工厂。每次尝试refresh()时都会调用。* 默认实现会创建一个DefaultListableBeanFactory,其中,getInternalParentBeanFactory()是这个上下文的父bean工厂。* 可以在子类中重写,例如自定义DefaultListableBeanFactory的设置。*/
protected DefaultListableBeanFactory createBeanFactory() {return new DefaultListableBeanFactory(getInternalParentBeanFactory());
}
3:定制此上下文使用的内部bean工厂,向BeanFactory设置setAllowBeanDefinitionOverriding和setAllowCircularReferences
/*** 定制此上下文使用的内部bean工厂,向BeanFactory设置setAllowBeanDefinitionOverriding和setAllowCircularReferences*/
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {if (this.allowBeanDefinitionOverriding != null) // eg1:allowBeanDefinitionOverriding=nullbeanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);if (this.allowCircularReferences != null) // eg2:allowCircularReferences=nullbeanFactory.setAllowCircularReferences(this.allowCircularReferences);
}
4:加载BeanDefinition
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);beanDefinitionReader.setEnvironment(this.getEnvironment());beanDefinitionReader.setResourceLoader(this);beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));// 对beanDefinitionReader设置setValidatinginitBeanDefinitionReader(beanDefinitionReader);// 加载xml配置信息loadBeanDefinitions(beanDefinitionReader);
}
// 对beanDefinitionReader设置setValidating
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {// eg1:validating=truereader.setValidating(this.validating);
}
// 加载xml配置信息
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {// eg1:ClassPathXmlApplicationContext#getConfigResources()Resource[] configResources = getConfigResources();if (configResources != null) // eg1: configResources=nullreader.loadBeanDefinitions(configResources);String[] configLocations = getConfigLocations();if (configLocations != null) // eg1: configLocations=["bean.xml"]reader.loadBeanDefinitions(configLocations);
}
// eg1: locations=["bean.xml"]
@Override
public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {Assert.notNull(locations, "Location array must not be null");int count = 0;for (String location : locations)count += loadBeanDefinitions(location);return count;
}
@Override
public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {return loadBeanDefinitions(location, null);
}/*** 从指定的资源位置(resource location)加载bean定义。* 位置(location)也可以是位置模式(location pattern),前提是这个bean definition reader的ResourceLoader是一个ResourcePatternResolver。*/
// eg1:location="bean.xml" actualResources=null
public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {ResourceLoader resourceLoader = getResourceLoader();// eg1:resourceLoader=ClassPathXmlApplicationContext@2bec854fif (resourceLoader == null)throw new BeanDefinitionStoreException("Cannot load bean definitions from location [" + location + "]: no ResourceLoader available");if (resourceLoader instanceof ResourcePatternResolver) {try {// eg1:location="bean.xml",AbstractApplicationContext#getResources(location)Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);// eg1:resources=[ClassPathContextResource]int count = loadBeanDefinitions(resources);if (actualResources != null) // eg1:actualResources=nullCollections.addAll(actualResources, resources);return count;}catch (IOException ex) {throw new BeanDefinitionStoreException("Could not resolve bean definition resource pattern [" + location + "]", ex);}}else {Resource resource = resourceLoader.getResource(location);int count = loadBeanDefinitions(resource);if (actualResources != null) actualResources.add(resource);return count;}
}
// eg1:resources=[ClassPathContextResource]
@Override
public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {Assert.notNull(resources, "Resource array must not be null");int count = 0;for (Resource resource : resources) {// 加载配置count += loadBeanDefinitions(resource); // eg1: XmlBeanDefinitionReader#loadBeanDefinitions(resource)}return count;
}
/*** 从指定的XML文件中加载bean定义。*/
// eg1:resource=ClassPathContextResource
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {return loadBeanDefinitions(new EncodedResource(resource));
}
代码走到这一步,就跟xmlBeanFactory方法重合了。
obtainFreshBeanFactory() 的 2:获得BeanFactory
@Override
public final ConfigurableListableBeanFactory getBeanFactory() {DefaultListableBeanFactory beanFactory = this.beanFactory;if (beanFactory == null) // eg1:beanFactory=DefaultListableBeanFactory@6e01f9b0throw new IllegalStateException("BeanFactory not initialized or already closed - " +"call 'refresh' before accessing beans via the ApplicationContext");return beanFactory;
}
/** 3:prepareBeanFactory 准备用于此上下文的beanFactory */
/*** 配置工厂的标准上下文特征,例如上下文的类加载器(context's ClassLoader)和后处理器(post-processors)*/
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {/** 1: 设置beanFactory的classLoader */beanFactory.setBeanClassLoader(getClassLoader()); // eg1:DefaultResourceLoader#getClassLoader()/** 2: 是否支持SpEL表达式解析,即:可以使用#{bean.xxx}的形式来调用相关属性值 */if (!shouldIgnoreSpel) // eg1:shouldIgnoreSpel=falsebeanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));/** 3: 添加1个属性编辑器,它是对bean的属性等设置管理的工具 */beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));/** 4: 添加1个bean的后置处理器 */beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));/** 5: 设置7个需要忽略自动装配的接口,维护到ignoredDependencyInterfaces中 */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.ignoreDependencyInterface(ApplicationStartupAware.class);/** 6: 注册4个依赖,向resolvableDependencies中注册类及实现类*/beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);beanFactory.registerResolvableDependency(ResourceLoader.class, this);beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);beanFactory.registerResolvableDependency(ApplicationContext.class, this);/** 7: 添加1个bean的后置处理器 */beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));/** 8: 增加对AspectJ的支持 */// eg1: NativeDetector.inNativeImage()=false, beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)=falseif (!NativeDetector.inNativeImage() && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));}/** 9: 注册4个默认的系统环境bean */if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) // eg1: 注册"environment"名称的beanbeanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) // eg1: 注册"systemProperties"名称的beanbeanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) // eg1: 注册"systemEnvironment"名称的beanbeanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());if (!beanFactory.containsLocalBean(APPLICATION_STARTUP_BEAN_NAME)) // eg1: 注册"applicationStartup"名称的beanbeanFactory.registerSingleton(APPLICATION_STARTUP_BEAN_NAME, getApplicationStartup());
}
prepareBeanFactory方法的主要作用是配置工厂的标准上下文特征,如上下文的类加载 器和后处理器。操作步骤如下所述:
① 设置beanFactory的classLoader
/*** 返回用来加载类路径资源的类加载器(ClassLoader)。* 将被传递给这个资源加载器创建的所有ClassPathResource对象的ClassPathResource构造函数。*/
@Override
@Nullable
public ClassLoader getClassLoader() {// eg1:classLoader=nullreturn (this.classLoader != null ? this.classLoader : ClassUtils.getDefaultClassLoader());
}
/*** 返回要使用的默认类加载器:通常是线程上下文的类加载器(如果有的话);加载ClassUtils类的类加载器将用作备用。* 如果你想使用线程上下文的类加载器,而你显然更喜欢非空的类加载器引用,那么可以调用这个方法:例如,用于类路径资源加载(但不一定用于类。* forName也接受一个null类加载器引用)。*/
@Nullable
public static ClassLoader getDefaultClassLoader() {ClassLoader cl = null;try {cl = Thread.currentThread().getContextClassLoader(); // 1:获得当前线程上下文的ClassLoader} catch (Throwable ex) {}if (cl == null) {cl = ClassUtils.class.getClassLoader(); // 2:获得ClassUtils类的ClassLoaderif (cl == null) {try {cl = ClassLoader.getSystemClassLoader(); // 3:获得系统的类加载器} catch (Throwable ex) {}}}return cl;
}
(上面代码块:SPI破话双亲委派机制)
② 是否支持SpEL表达式解析,即:可以使用#{bean.xxx}的形式来调用相关属性值
SpEL全称是“Spring Expression Language”,它能在运行时构件复杂表达式、存取对象图 属性、对象方法调用等;它是单独模块,只依赖了core模块,所以可以单独使用。SpEL 使用 #{...} 作为界定符,所有在大括号中的字符都会被认定为SpEL,使用方式如下所示:
在prepareBeanFactory(beanFactory)方法中,通过beanFactory类的 setBeanExpressionResolver(new StandardBeanExpressionResolver(...))方法注册SpEL语言 解析器,就可以对SpEL进行解析了。
那么,在注册了解析器后,Spring又是在什么时候调用这个解析器进行解析操作的呢?调 用方式如下图所示:
其实就是在Spring进行bean初始化的时候,有一个步骤是——属性填充(即:populateBean()), 而在这一步中Spring会调用AbstractAutowireCapableBeanFactory#applyPropertyValues(...)方 法来完成功能。而就在这个方法中,会创建BeanDefinitionValueResolver实例对象valueResolver 来进行属性值的解析操作。同时,也是在这个步骤中,通过 AbstractBeanFactory#evaluateBeanDefinitionString(...)方法去完成SpEL的解析操作。
(视频6-5:1h50min)
③ 添加1个属性编辑器ResourceEditorRegistrar,对bean的属性等设置管理的工具
什么是属性编辑器呢?在Spring加载bean的时候,可以把基本类型属性注入进来,但是 对于类似Date这种复杂属性就无法被识别了,不过可以通过属性编辑器解决。
因为date 属性是Date类型的,但是给配置的属性值是字符串类型的,所以需要进行一下转换。
以上例子:
@Data
public class Schedule {private String name;private Date date; // 添加Date类型的属性
}
public class DatePropertyEditor implements PropertyEditorRegistrar {@Overridepublic void registerCustomEditors(PropertyEditorRegistry registry) {registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));}
}
<!-- 属性编辑器演示 -->
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"><property name="propertyEditorRegistrars"><list><bean class="com.muse.springdemo.propertyeditor.DatePropertyEditor"/></list></property>
</bean>
<bean id="schedule" class="com.muse.springdemo.propertyeditor.Schedule"><property name="name" value="work"/><property name="date" value="2023-01-01"/> <!-- date是Date类型,需要将String转换为Date -->
</bean>
@Test
void testPropertyEditor() {Schedule schedule = applicationContext.getBean("schedule", Schedule.class);log.info("schedule={}", schedule);
}
运行结果:
属性编辑器源码解析:
通过上面对于注册属性编辑器的配置,我们可以看到自定义的属性编辑器 DatePropertyEditor被保存到了CustomEditorConfigurer的propertyEditorRegistrars属性 中,那么由于CustomEditorConfigurer类实现了BeanFactoryPostProcessor接口,所以当 bean初始化的时候,会调用它的postProcessBeanFactory(...)方法,那么在这个方法中, 会将propertyEditorRegistrars属性中的所有属性编辑器添加到beanFactory中。
分析到这一步之后,发现自定义属性编辑器都会保存到beanFactory的变量 propertyEditorRegistrars中。那么问题来了——保存我们知道了,那什么时候属性编辑 器会被调用呢?其实就是在初始化bean的时候,在initBeanWrapper(...)方法中,会被调用。
④ 添加1个bean的后置处理器ApplicationContextAwareProcessor
⑤ 设置7个需要忽略自动装配的接口,维护到ignoredDependencyInterfaces中
⑥ 注册4个依赖,向resolvableDependencies中注册类及实现类
⑦ 添加1个bean的后置处理器ApplicationListenerDetector
⑧ 增加对AspectJ的支持 ⑨ 注册4个默认的系统环境bean
/** 4:激活各种BeanFactoryPostProcessor的后置处理器 */
invokeBeanFactoryPostProcessors(beanFactory);
/** 5:注册各种Bean的后置处理器,在getBean时才会被调用 */
registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
/** 6:为上下文初始化消息源(即:国际化处理)*/
initMessageSource();
/** 7:为上下文初始化应用事件广播器 */
initApplicationEventMulticaster();
onRefresh(); // 初始化特定上下文子类中的其他特殊bean(空方法,可由子类实现)
/** 8:在所有注册的bean中查找listener bean,并注册到消息广播器中 */
registerListeners();
/** 9:初始化剩下的单例(非惰性non-lazy-init)*/
finishBeanFactoryInitialization(beanFactory);
/** 10:完成refresh,通知lifecycleProcessor刷新过程,同时发出ContextRefreshEvent通知 */
finishRefresh();