完整路径:
调用setText方法
//程序入口
MyClassPathXmlApplicationContext.MyClassPathXmlApplicationContext(String... configLocations){}
ClassPathXmlApplicationContext(String... configLocations) throws BeansException {}
ClassPathXmlApplicationContext.ClassPathXmlApplicationContext(String... configLocations) throws BeansException {}
this(configLocations, true, null);
#重复方法, enter
this(configLocations, true, null);
refresh();
#spring核心代码#refresh();
//Spring源码核心,所有初始化信息的加载过程在全部都在这里。
// 初始化剩下的单实例(非懒加载的)
finishBeanFactoryInitialization(beanFactory);
#方法调用
// 实例化剩下的单例对象
beanFactory.preInstantiateSingletons();
ConfigurableListableBeanFactory.preInstantiateSingletons() throws BeansException;
DefaultListableBeanFactory.preInstantiateSingletons() throws BeansException {}
// 如果beanName对应的bean不是FactoryBean,只是普通的bean,通过beanName获取bean实例
getBean(beanName);
AbstractBeanFactory.getBean(String name) throws BeansException {}
// 此方法是实际获取bean的方法,也是触发依赖注入的方法
return doGetBean(name, null, null, false);
#方法调用
// 为给定的合并后BeanDefinition(和参数)创建一个bean实例
return createBean(beanName, mbd, args);
AbstractBeanFactory.createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException;
AbstractAutowireCapableBeanFactory.createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {}
// 实际创建bean的调用
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
#方法调用beanName.equals("customer"),此处是 循环执行,需要等对象是customer才是自定义解析器。
// 对bean的属性进行填充,将各个属性值注入,其中,可能存在依赖于其他bean的属性,则会递归初始化依赖的bean
populateBean(beanName, mbd, instanceWrapper);
#方法调用
//应用给定的属性值,解决任何在这个bean工厂运行时其他bean的引用。必须使用深拷贝,所以我们 不会永久地修改这个属性
applyPropertyValues(beanName, mbd, bw, pvs);
#方法调用propertyName.equals("address"),此处是 循环执行,需要等对象是customer才是自定义解析器。
//将resolvedValue转换为指定的目标属性对象
convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter);
#方法调用
return ((BeanWrapperImpl) converter).convertForProperty(value, propertyName);
BeanWrapperImpl.convertForProperty(@Nullable Object value, String propertyName) throws TypeMismatchException {}
return convertForProperty(propertyName, null, value, td);
AbstractNestablePropertyAccessor.convertForProperty(
String propertyName, @Nullable Object oldValue, @Nullable Object oldValue, TypeDescriptor td)
throws TypeMismatchException {}
return convertIfNecessary(propertyName, oldValue, newValue, td.getType(), td);
#调用方法
return this.typeConverterDelegate.convertIfNecessary(propertyName, oldValue, newValue, requiredType, td);
TypeConverterDelegate.convertIfNecessary(@Nullable String propertyName, @Nullable Object oldValue, @Nullable Object newValue,
@Nullable Class<T> requiredType, @Nullable TypeDescriptor typeDescriptor) throws IllegalArgumentException {}
// 使用editor将convertedValue转换为requiredType
convertedValue = doConvertValue(oldValue, convertedValue, requiredType, editor);
#方法调用
// 使用editor转换newTextValue,并将转换后的值返回出去
return doConvertTextValue(oldValue, newTextValue, editor);
#方法调用
// 使用newTextValue更新内部属性值
editor.setAsText(newTextValue);
#路径结束#自定义属性解析器
AddressPropertyEditor.setAsText(String text) throws IllegalArgumentException {}
//程序入口
MyClassPathXmlApplicationContext.MyClassPathXmlApplicationContext(String... configLocations){}
ClassPathXmlApplicationContext(String... configLocations) throws BeansException {}
ClassPathXmlApplicationContext.ClassPathXmlApplicationContext(String... configLocations) throws BeansException {}
this(configLocations, true, null);
#重复方法, enter
this(configLocations, true, null);
refresh();
#spring核心代码#refresh();
//Spring源码核心,所有初始化信息的加载过程在全部都在这里。
// 初始化剩下的单实例(非懒加载的)
finishBeanFactoryInitialization(beanFactory);
#方法调用
// 实例化剩下的单例对象
beanFactory.preInstantiateSingletons();
ConfigurableListableBeanFactory.preInstantiateSingletons() throws BeansException;
DefaultListableBeanFactory.preInstantiateSingletons() throws BeansException {}
// 如果beanName对应的bean不是FactoryBean,只是普通的bean,通过beanName获取bean实例
getBean(beanName);
AbstractBeanFactory.getBean(String name) throws BeansException {}
// 此方法是实际获取bean的方法,也是触发依赖注入的方法
return doGetBean(name, null, null, false);
#方法调用
// 为给定的合并后BeanDefinition(和参数)创建一个bean实例
return createBean(beanName, mbd, args);
AbstractBeanFactory.createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException;
AbstractAutowireCapableBeanFactory.createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {}
// 实际创建bean的调用
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
#方法调用
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {}
// 根据执行bean使用对应的策略创建新的实例,如,工厂方法,构造函数主动注入、简单初始化
instanceWrapper = createBeanInstance(beanName, mbd, args);
#调用方法
// 使用默认无参构造函数创建对象,如果没有无参构造且存在多个有参构造且没有@AutoWired注解构造,会报错
return instantiateBean(beanName, mbd);
#调用方法
// 包装成BeanWrapper
BeanWrapper bw = new BeanWrapperImpl(beanInstance);
####读取解析器Edidor####代码分支
#initBeanWrapper(bw);
BeanWrapperImpl.BeanWrapperImpl(Object object) {}
super(object);
AbstractNestablePropertyAccessor.AbstractNestablePropertyAccessor(Object object) {}
registerDefaultEditors();
#路径结束#
PropertyEditorRegistrySupport.registerDefaultEditors() {}
this.defaultEditorsActive = true;
####################################################################
####读取解析器Edidor####代码分支
#BeanWrapper bw = new BeanWrapperImpl(beanInstance);
initBeanWrapper(bw);
#方法调用
// 将工厂中所有PropertyEditor注册到bw中
registerCustomEditors(bw);
#方法调用
// 将registrar中的所有PropertyEditor注册到PropertyEditorRegistry中
registrar.registerCustomEditors(registry);
#路径结束#
ResourceEditorRegistrar.registerCustomEditors(PropertyEditorRegistry registry) {}
相关类
调用方法
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// 设置beanfactory的表达式语言处理器
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
}
解析表达式#{}
package org.springframework.context.expression;
StandardBeanExpressionResolver.java
调用方法
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// 为beanFactory增加一个默认的propertyEditor,这个主要是对bean的属性等设置管理的一个工具类
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
}
注册解析器Editor
package org.springframework.beans.support
ResourceEditorRegistrar
public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
@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));
}
}
}
备注:
beanName.equals(“customer”),此处是 循环执行,需要等对象是customer才是自定义解析器。
package org.springframework.beans.factory.support
AbstractAutowireCapableBeanFactory.java
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
// 对bean的属性进行填充,将各个属性值注入,其中,可能存在依赖于其他bean的属性,则会递归初始化依赖的bean
populateBean(beanName, mbd, instanceWrapper);
}
propertyName.equals(“address”),此处是 循环执行,需要等对象是customer才是自定义解析器。
package org.springframework.beans.factory.support
AbstractAutowireCapableBeanFactory.java
protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
//将resolvedValue转换为指定的目标属性对象
convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter);
}
调试操作:
第一次进入调试,只有保留beanName和propertyName入口代码的注释。进入了propertyName.equals(“address”);逻辑后开启所有注释。
spring 5.2.9 07 源码分析-spring的bean工厂准备工作 断点注释
https://download.csdn.net/download/weixin_50750933/85749549