我们知道spring的自动注入有两种方式:1.xml 2.@Autowired
1.如果是xml方式:
定义这样一个xml配置文件,定义两个bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="a" class="com.stepfive.pojo.A" >
<property name="b" ref="b" />
</bean>
<bean id="b" class="com.stepfive.pojo.B" >
<property name="a" ref="a"/>
</bean>
</beans>
在doCreateBean方法中:
属性填充的方法(属性初始化):
populateBean(beanName, mbd, instanceWrapper);
populateBean()方法中:
PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
这个方法中可以获取到所有的属性和值的集合,之后填充beanB的过程见spring解决循环依赖
那么这个pvs是从哪里来的呢?它其实是beandefinition的一部分,那我们知道benadefinition是beanfactory的一个非常重要的成员,构建bean实例化bean就是靠得到它,而它是在refresh中的哪一步构建出来的呢?通过断点跟踪,得出结论,是在构建beanfactory的过程中:
// Tell the subclass to refresh the internal bean factory.
//创建beanfactory对象
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
//在初始化beanfactory对象的过程中,由这么一个方法:
//加载beandefinition
//其内部通过xml解析的方式,得到了beandefinition
loadBeanDefinitions(beanFactory);
//创建出来之后肯定是要被放进缓存下次接着用拉
this.beanDefinitionMap.put(beanName, beanDefinition);
所以,最终要populatebean的时候取出来的pvs其实就是初始化beanfactory的时候被创建出来的啦
2.@Autowired
注解方式下,pvs是null或者会是空的,所以填充属性的过程不是像xml那种通过pplyPropertyValues来赋值的,而是通过后处理器:
PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
通过AutowiredBeanPostProcessor的inject方法实现自动注入:
@Override
protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
Field field = (Field) this.member;
Object value;
if (this.cached) {
value = resolvedCachedArgument(beanName, this.cachedFieldValue);
}
else {
DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
desc.setContainingClass(bean.getClass());
Set<String> autowiredBeanNames = new LinkedHashSet<>(1);
Assert.state(beanFactory != null, "No BeanFactory available");
TypeConverter typeConverter = beanFactory.getTypeConverter();
try {
value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
}
catch (BeansException ex) {
throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex);
}
synchronized (this) {
if (!this.cached) {
if (value != null || this.required) {
this.cachedFieldValue = desc;
registerDependentBeans(beanName, autowiredBeanNames);
if (autowiredBeanNames.size() == 1) {
String autowiredBeanName = autowiredBeanNames.iterator().next();
if (beanFactory.containsBean(autowiredBeanName) &&
beanFactory.isTypeMatch(autowiredBeanName, field.getType())) {
this.cachedFieldValue = new ShortcutDependencyDescriptor(
desc, autowiredBeanName, field.getType());
}
}
}
else {
this.cachedFieldValue = null;
}
this.cached = true;
}
}
}
if (value != null) {
ReflectionUtils.makeAccessible(field);
field.set(bean, value);
}
}
那注解方式下beandefinitionmap是怎么初始化的呢?(猜测是我们加了配置类@Configuration,然后在配置类上加了@ComponentScan进行包扫描获得的吧?)
所以,@Configuration的作用?
应该是beanfactorypostprocessor吧。。。