@Autowired注解的作用

我们知道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吧。。。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_34116044

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值