Spring 自动装配之依赖注入 DI运行时序
- 实例化
- 寻找获取 Bean 的入口
- BeanFactory 类
- getBean()
- AbstractBeanFactory 类
- doGetBean()
- 开始实例化
- AbstractAutowireCapableBeanFactory 类
- createBean()
- doCreateBean()
- 选择 Bean 实例化策略
- createBeanInstance()
- instantiateBean()
- 执行 Bean 实例化
- SimpleInstantiationStrategy 类
- instantiate()
- CglibSubclassingInstantiationStrategy 类
- instantiateWithMethodInjection()
- instantiate()
- 依赖注入
- 准备依赖注入
- AbstractAutowireCapableBeanFactory 类
- populateBean()
- applyPropertyValues()
- 解析属性注入规则
- BeanDefinitionValueResolver 类
- resolveValueIfNecessary()
- 注入赋值
- AbstractPropertyAccessor 类
- setPropertyValues()
- setPropertyValue()
- AbstractNestablePropertyAccessor 类
- setPropertyValue()
- processKeyedProperty()
- getPropertyHoldingValue()
实例化
寻找获取 Bean 的入口
BeanFactory 类
getBean()
/**
* 根据bean的名字,获取在IOC容器中得到bean实例
* @param name
* @return
* @throws BeansException
*/
Object getBean(String name) throws BeansException;
AbstractBeanFactory 类
/**
* 获取IOC容器中指定 名称 的Bean
* @param name the name of the bean to retrieve
* @return
* @throws BeansException
*/
@Override
public Object getBean(String name) throws BeansException {
//doGetBean才是真正向IoC容器获取被管理Bean的过程
return doGetBean(name, null, null, false);
}
/**
* 获取IOC容器中指定 名称 和 类型 的Bean
* @param name the name of the bean to retrieve
* @param requiredType type the bean must match. Can be an interface or superclass
* of the actual class, or {@code null} for any match. For example, if the value
* is {@code Object.class}, this method will succeed whatever the class of the
* returned instance.
* @param <T>
* @return
* @throws BeansException
*/
@Override
public <T> T getBean(String name,
@Nullable Class<T> requiredType) throws BeansException {
//doGetBean才是真正向IoC容器获取被管理Bean的过程
return doGetBean(name, requiredType, null, false);
}
/**
* 获取IOC容器中指定 名称 和 参数 的Bean
* @param name the name of the bean to retrieve
* @param args arguments to use when creating a bean instance using explicit arguments
* (only applied when creating a new instance as opposed to retrieving an existing one)
* @return
* @throws BeansException
*/
@Override
public Object getBean(String name, Object... args) throws BeansException {
//doGetBean才是真正向IoC容器获取被管理Bean的过程
return doGetBean(name, null, args, false);
}
/**
* 获取IOC容器中指定 名称、类型 和 参数 的Bean
* @param name
* @param requiredType
* @param args
* @param <T>
* @return
* @throws BeansException
*/
public <T> T getBean(String name,
@Nullable Class<T> requiredType,
@Nullable Object... args)
throws BeansException {
//doGetBean才是真正向IoC容器获取被管理Bean的过程
return doGetBean(name, requiredType, args, false);
}
时序图 :
doGetBean()
/**
* 真正实现向IOC容器获取Bean的功能,也是触发依赖注入功能的地方
*/
@SuppressWarnings("unchecked")
protected <T> T doGetBean(final String name,
@Nullable final Class<T> requiredType,
@Nullable final Object[] args,
boolean typeCheckOnly) throws BeansException {
//IOC容器创建单例模式Bean实例对象
if (sharedInstance != null && args == null) {
}
else {
// 创建单例模式Bean的实例对象
if (mbd.isSingleton()) {
//创建一个指定Bean实例对象,如果有父级继承,则合并子类和父类的定义
return createBean(beanName, mbd, args);
}
// //IOC容器创建原型模式Bean实例对象
else if (mbd.isPrototype()){
//创建指定Bean对象实例
prototypeInstance = createBean(beanName, mbd, args);
}
// 要创建的Bean既不是单例模式,也不是原型模式,
// 则根据Bean定义资源中配置的生命周期范围,选择实例化Bean的合适方法,
// 这种在Web应用程序中比较常用,
// 如:request、session、application等生命周期
else {
return createBean(beanName, mbd, args);
}
}
}
时序图 :
开始实例化
AbstractAutowireCapableBeanFactory 类
createBean()
/**
* 创建Bean实例对象
* @param beanName the name of the bean
* @param mbd the merged bean definition for the bean
* @param args explicit arguments to use for constructor or factory method invocation
* @return
* @throws BeanCreationException
*/
@Override
protected Object createBean(String beanName,
RootBeanDefinition mbd,
@Nullable Object[] args)
throws BeanCreationException {
//创建Bean的入口
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
}
doCreateBean()
/**
* 真正创建Bean的方法
* @param beanName
* @param mbd
* @param args
* @return
* @throws BeanCreationException
*/
protected Object doCreateBean(final String beanName,
final RootBeanDefinition mbd,
final @Nullable Object[] args)
throws BeanCreationException {
if (instanceWrapper == null) {
// 生成 Bean 所包含的 java 对象实例
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
//将Bean实例对象封装,并且Bean定义中配置的属性值赋值给实例对象
populateBean(beanName, mbd, instanceWrapper);
}
选择 Bean 实例化策略
createBeanInstance()
/**
* 创建Bean的实例对象
* @param beanName
* @param mbd
* @param args
* @return
*/
protected BeanWrapper createBeanInstance(String beanName,
RootBeanDefinition mbd,
@Nullable Object[] args) {
//使用默认的无参构造方法实例化
return instantiateBean(beanName, mbd);
}
instantiateBean()
/**
* 使用默认的无参构造方法实例化Bean对象
* @param beanName
* @param mbd
* @return
*/
protected BeanWrapper instantiateBean(final String beanName,
final RootBeanDefinition mbd) {
//将实例化的对象封装起来
beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent);
}
执行 Bean 实例化
SimpleInstantiationStrategy 类
instantiate()
/**
* 使用初始化策略实例化Bean对象
*
* @param bd the bean definition
* @param beanName the name of the bean when it's created in this context.
* The name can be {@code null} if we're autowiring a bean which doesn't
* belong to the factory.
* @param owner the owning BeanFactory
* @return
*/
@Override
public Object instantiate(RootBeanDefinition bd,
@Nullable String beanName,
BeanFactory owner) {
//如果Bean定义中没有方法覆盖,则就不需要CGLIB父类类的方法
if (!bd.hasMethodOverrides()) {
}
else {
//使用CGLIB来实例化对象
return instantiateWithMethodInjection(bd, beanName, owner);
}
}
CglibSubclassingInstantiationStrategy 类
instantiateWithMethodInjection()
@Override
protected Object instantiateWithMethodInjection(RootBeanDefinition bd,
@Nullable String beanName,
BeanFactory owner) {
return instantiateWithMethodInjection(bd, beanName, owner, null);
}
@Override
protected Object instantiateWithMethodInjection(RootBeanDefinition bd,
@Nullable String beanName,
BeanFactory owner,
@Nullable Constructor<?> ctor,
@Nullable Object... args) {
// Must generate CGLIB subclass...
return new CglibSubclassCreator(bd, owner).instantiate(ctor, args);
}
instantiate()
/**
* 使用CGLIB进行Bean对象实例化
* @param ctor
* @param args
* @return
*/
public Object instantiate(@Nullable Constructor<?> ctor, @Nullable Object... args) {
//创建代理子类
Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
}
依赖注入
准备依赖注入
AbstractAutowireCapableBeanFactory 类
populateBean()
/**
* 将Bean属性设置到生成的实例对象上
* @param beanName
* @param mbd
* @param bw
*/
protected void populateBean(String beanName,
RootBeanDefinition mbd,
@Nullable BeanWrapper bw) {
if (pvs != null) {
//对属性进行注入
applyPropertyValues(beanName, mbd, bw, pvs);
}
}
applyPropertyValues()
/**
* 解析并注入依赖属性的过程
* @param beanName
* @param mbd
* @param bw
* @param pvs
*/
protected void applyPropertyValues(String beanName,
BeanDefinition mbd,
BeanWrapper bw,
PropertyValues pvs) {
//属性值不需要转换
if (pv.isConverted()) {
}
//属性值需要转换
else {
//转换属性值,例如将引用转换为IOC容器中实例化对象引用
Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);
}
//进行属性依赖注入
bw.setPropertyValues(new MutablePropertyValues(deepCopy));
}
解析属性注入规则
BeanDefinitionValueResolver 类
resolveValueIfNecessary()
/**
* 解析属性值,对注入类型进行转换
* @param argName
* @param value
* @return
*/
@Nullable
public Object resolveValueIfNecessary(Object argName,
@Nullable Object value) {
//对引用类型的属性进行解析
if (value instanceof RuntimeBeanReference) {
//调用引用类型属性的解析方法
return resolveReference(argName, ref);
}
//对集合数组类型的属性解析
else if (value instanceof ManagedArray) {
//创建指定类型的数组
return resolveManagedArray(argName, (List<?>) value, elementType);
}
}
注入赋值
AbstractPropertyAccessor 类
setPropertyValues()
@Override
public void setPropertyValues(PropertyValues pvs) throws BeansException {
setPropertyValues(pvs, false, false);
}
@Override
public void setPropertyValues(PropertyValues pvs,
boolean ignoreUnknown,
boolean ignoreInvalid)
throws BeansException {
setPropertyValue(pv);
}
setPropertyValue()
@Override
public void setPropertyValue(PropertyValue pv) throws BeansException {
setPropertyValue(pv.getName(), pv.getValue());
}
AbstractNestablePropertyAccessor 类
setPropertyValue()
@Override
public void setPropertyValue(String propertyName, @Nullable Object value) throws BeansException {
nestedPa.setPropertyValue(tokens, new PropertyValue(propertyName, value));
}
/**
* 实现属性依赖注入功能
* @param tokens
* @param pv
* @throws BeansException
*/
protected void setPropertyValue(PropertyTokenHolder tokens,
PropertyValue pv) throws BeansException {
if (tokens.keys != null) {
processKeyedProperty(tokens, pv);
}
else {
processLocalProperty(tokens, pv);
}
}
processKeyedProperty()
/**
* 实现属性依赖注入功能
* @param tokens
* @param pv
*/
@SuppressWarnings("unchecked")
private void processKeyedProperty(PropertyTokenHolder tokens,
PropertyValue pv) {
//调用属性的getter方法,获取属性的值
Object propValue = getPropertyHoldingValue(tokens);
}
getPropertyHoldingValue()
private Object getPropertyHoldingValue(PropertyTokenHolder tokens) {
//获取属性值
propValue = getPropertyValue(getterTokens);
}