Spring源码阅读 - DI
DI
① 实例化:
② 依赖注入:
PS : Spring中,单例对象默认是非延时加载的,而原型对象默认是延时加载的。
AbstractBeanFactory:
AbstractBeanFactory#getBean()
:尝试获取bean,如果缓存中存在着直接返回,否则进入创建逻辑
// DI 1 :对象实例化和依赖注入的入口
AbstractBeanFactory#getBean()
AbstractBeanFactory#doGetBean()
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
// DI 2: 先从缓存中取Bean --- 三层缓存依次判断(为解决循环依赖问题)
Object sharedInstance = getSingleton(beanName);
// 如果缓存中存在单例Bean,则进行相关判断之后直接返回Bean
if (sharedInstance != null && args == null) {
if (logger.isDebugEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
}
}
// DI 3:该方法主要涉及当sharedInstance为factoryBean时的相关逻辑处理
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
} else {
...
// Create bean instance.
//创建单例模式Bean的实例对象
if (mbd.isSingleton()) {
//这里使用了一个匿名内部类,创建Bean实例对象,并且注册给所依赖的对象
sharedInstance = getSingleton(beanName, () -> {
try {
//DI 3: 创建一个指定Bean实例对象,如果有父级继承,则合并子类和父类的定义
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
//显式地从容器单例模式Bean缓存中清除实例对象
destroySingleton(beanName);
throw ex;
}
});
//获取给定Bean的实例对象
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
...
}
AbstractAutowireCapableBeanFactory#createBean()
:创建对象
AbstractAutowireCapableBeanFactory#createBean():
// DI 4:生命周期
// 如果InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation()前置处理器,
// 结果返回了一个代理bean,则进入BeanPostProcessor.applyBeanPostProcessorsAfterInitialization()完成后置处理
// 然后直接返回该bean对象,不进入后面的创建逻辑
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
// DI 5:创建Bean的入口
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
AbstractAutowireCapableBeanFactory#doCreateBean():
// DI 6: 创建实例对象
instanceWrapper = createBeanInstance(beanName, mbd, args);
// DI 7: 依赖注入bean - 将Bean实例对象封装,并且Bean定义中配置的属性值赋值给实例对象
populateBean(beanName, mbd, instanceWrapper);
// DI 6: 创建实例对象
AbstractAutowireCapableBeanFactory#createBeanInstance():
AbstractAutowireCapableBeanFactory#instantiateBean():
// DI 8 : 如果存在方法替换(replace-method或lookup-method),则需要使用cglib,否则使用JDK反射篡创建对象。
AbstractAutowireCapableBeanFactory#getInstantiationStrategy().instantiate(mbd, beanName, parent);
// DI 7: 依赖注入bean
AbstractAutowireCapableBeanFactory#populateBean():
//MutablePropertyValues newPvs = new MutablePropertyValues(pvs) // 依赖配置处理
AbstractAutowireCapableBeanFactory#applyPropertyValues();
//BeanDefinitionValueResolver.resolveValueIfNecessary(); // 值转换
// DI 9:为实例对象设置属性值
BeanWrapper.setPropertyValues(mpvs);
//AbstractNestablePropertyAccessor#setPropertyValue()
protected void setPropertyValue(PropertyTokenHolder tokens, PropertyValue pv) throws BeansException {
if (tokens.keys != null) {
// DI 10:为实例对象设置属性值 - 集合类型
processKeyedProperty(tokens, pv);
}
else {
// DI 10:为实例对象设置属性值 - 反射调用setter方法注入
processLocalProperty(tokens, pv);
}
}
欢迎疑问、期待评论、感谢指点 -- kiqi,愿同您为友
-- 星河有灿灿,愿与之辉