Spring源码解析 Bean属性填充

目录
  • 前言
  • 属性填充
  • 执行回调方法及后置处理器

前言

在上一篇文章中,我们分析了Spring中Bean的实例化过程,在结尾我们知道了虽然bean的实例化完成了,但是其中的属性还没有被注入,今天我们就接着来分析属性是如何被注入的。

属性填充

实例化完成后,回到上面第3条的doCreateBean方法中,看一下用BeanWrapper产生的原生对象,里面dao这个属性还是null值。

回归一下之前的代码,接下来要调用populateBean方法进行属性的填充:

Object exposedObject = bean;
try {
  populateBean(beanName, mbd, instanceWrapper);
  exposedObject = initializeBean(beanName, exposedObject, mbd);
}

看一下populateBean中的核心代码:

for (BeanPostProcessor bp : getBeanPostProcessors()) {
  if (bp instanceof InstantiationAwareBeanPostProcessor) {
    InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
    pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
    if (pvs == null) {
      return;
    }
  }
}

这里通过getBeanPostProcessors方法获得当前注册的所有后置处理器,如果属于InstantiationAwareBeanPostProcessor类型,则调用它的postProcessPropertyValues方法。通过遍历,可以知道当前spring中存在7个后置处理器:

我们主要来看一下AutowiredAnnotationBeanPostProcessor,因为它负责对添加了 @Autowired@Value等注解的属性进行依赖的填充。进入它的postProcessPropertyValues方法:

public PropertyValues postProcessPropertyValues(
  PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeanCreationException {
  InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs);
  try {
    metadata.inject(bean, beanName, pvs);
  }
 //异常处理代码省略...
  return pvs;
}

这里的InjectionMetadata可以理解为要注入的属性的元数据,在它里面维护了一个Collection,来存放所有需要注入的bean:

private final Collection<InjectedElement> injectedElements;

进入findAutowiringMetadata方法:

private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) {
 String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
 InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
  //省略非重要代码...
 return metadata;
}

在执行完这一步后,就把需要填充的属性放进了刚才提到的injectedElements中:

接下来,继续执行InjectionMetadatainject方法,在其中遍历所有需要注入的属性的列表,遍历调用AutowiredAnnotationBeanPostProcessor的inject方法:

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 {//用beanFactory解决依赖
          value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
        }
   //后面代码省略...

这里创建了一个DependencyDescriptor,用来维护注入属性与它的容器类containingClass的关系,里面最重要的就是存放了注入属性的类型、名称,以及containingClass的类型等信息。

调用resolveDependency方法,其中没有做什么实质性的工作,继续调用了doResolveDependency方法:

public Object doResolveDependency(DependencyDescriptor descriptor, @Nullable String beanName,
    @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException {
  InjectionPoint previousInjectionPoint = ConstructorResolver.setCurrentInjectionPoint(descriptor);
  try {
    Object shortcut = descriptor.resolveShortcut(this);
    if (shortcut != null) {
      return shortcut;
    }
    //依赖的属性值的类型
    Class<?> type = descriptor.getDependencyType();
    Object value = getAutowireCandidateResolver().getSuggestedValue(descriptor);
    if (value != null) {
      if (value instanceof String) {
        String strVal = resolveEmbeddedValue((String) value);
        BeanDefinition bd = (beanName != null && containsBean(beanName) ? getMergedBeanDefinition(beanName) : null);
        value = evaluateBeanDefinitionString(strVal, bd);
      }
      TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
      return (descriptor.getField() != null ?
          converter.convertIfNecessary(value, type, descriptor.getField()) :
          converter.convertIfNecessary(value, type, descriptor.getMethodParameter()));
    }

    Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter);
    if (multipleBeans != null) {
      return multipleBeans;
    }
    //把匹配的值和类型拿出来,放到一个map中
    Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor);
    if (matchingBeans.isEmpty()) {
      if (isRequired(descriptor)) {
        raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor);
      }
      return null;
    }

    String autowiredBeanName;
    Object instanceCandidate;
    //如果有超过一个匹配的,可能会有错误
    if (matchingBeans.size() > 1) {
      autowiredBeanName = determineAutowireCandidate(matchingBeans, descriptor);
      if (autowiredBeanName == null) {
        if (isRequired(descriptor) || !indicatesMultipleBeans(type)) {
          return descriptor.resolveNotUnique(type, matchingBeans);
        }
        else {
          return null;
        }
      }
      instanceCandidate = matchingBeans.get(autowiredBeanName);
    }
    else {
      Map.Entry<String, Object> entry = matchingBeans.entrySet().iterator().next();
      autowiredBeanName = entry.getKey();
      instanceCandidate = entry.getValue();
    }

    if (autowiredBeanNames != null) {
      //把找到的bean的名字放到set中
      autowiredBeanNames.add(autowiredBeanName);
    }
    if (instanceCandidate instanceof Class) {
      // 实际获取注入的bean
      instanceCandidate = descriptor.resolveCandidate(autowiredBeanName, type, this);
    }
    Object result = instanceCandidate;
    if (result instanceof NullBean) {
      if (isRequired(descriptor)) {
        raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor);
      }
      result = null;
    }
    if (!ClassUtils.isAssignableValue(type, result)) {
      throw new BeanNotOfRequiredTypeException(autowiredBeanName, type, instanceCandidate.getClass());
    }
    return result;
  }
  finally {
    ConstructorResolver.setCurrentInjectionPoint(previousInjectionPoint);
  }
}

通过findAutowireCandidates方法,获取与注入属性匹配的值和类型,放到一个Map当中,再通过它的beanName,调用resolveCandidate方法,实际获取注入的bean实例。这一操作底层调用的也是BeanFactory的getBean方法。

回到inject方法,使用反射将注入的bean实例赋值给属性:

ReflectionUtils.makeAccessible(field);
field.set(bean, value);

在执行完populateBean方法后,依赖的属性已经被注入成功了。

执行回调方法及后置处理器

在bean实例化完成后,执行各种回调和后置管理器方法:

protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
  if (System.getSecurityManager() != null) {
    AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
      invokeAwareMethods(beanName, bean);
      return null;
    }, getAccessControlContext());
  }
  else {
    //若bean实现了BeanNameAware、BeanClassLoaderAware、BeanFactoryAware接口,执行回调方法
    invokeAwareMethods(beanName, bean);
  }

  Object wrappedBean = bean;
  if (mbd == null || !mbd.isSynthetic()) {
    //执行所有后置处理器的before方法
    wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
  }

  try {
    //执行bean生命周期回调中的init-method
    //若bean实现了InitializingBean接口,执行afterPropertiesSet方法
    invokeInitMethods(beanName, wrappedBean, mbd);
  }
  catch (Throwable ex) {
    throw new BeanCreationException(
        (mbd != null ? mbd.getResourceDescription() : null),
        beanName, "Invocation of init method failed", ex);
  }
  if (mbd == null || !mbd.isSynthetic()) {
    //执行所有后置处理器的after方法
    wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
  }

  return wrappedBean;
}

具体执行内容:

  • 1、若bean实现了BeanNameAwareBeanClassLoaderAwareBeanFactoryAware接口,执行回调方法
  • 2、执行所有后置处理器的postProcessBeforeInitialization方法
  • 3、执行bean生命周期回调中的init-method,若bean实现了InitializingBean接口,执行afterPropertiesSet方法
  • 4、执行所有后置处理器的postProcessAfterInitialization方法

在这一步完成后,bean的实例化过程全部结束。最后执行一下refresh方法中的finishRefresh方法,进行广播事件等操作。到这,一个完整的AnnotationConfigApplicationContext初始化就完成了。

到此这篇关于Spring源码解析 Bean属性填充的文章就介绍到这了,更多相关Spring Bean属性填充内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Spring配置文件无法读取properties属性的解决

    目录 Spring配置文件无法读取properties @Value读取properties类型错误 Invalid boolean value [${spring.datasource.testWhileIdle}] eclipse解决方案 idea解决方案 Spring配置文件无法读取properties 在Spring项目的配置文件中引用properties属性文件中的属性,运行时无法识别properties属性文件中的属性引用,但properties属性文件和属性明明已经存在 例如: 要

  • 关于Spring Bean实例过程中使用反射和递归处理的Bean属性填充问题

    一.前言 超卖.掉单.幂等,你的程序总是不抗揍! 想想,运营已经对外宣传了七八天的活动,满心欢喜的等着最后一天页面上线对外了,突然出现了一堆异常.资损.闪退,而用户流量稍纵即逝,最后想死的心都有! 就编程开发来讲,丢三落四.乱码七糟,可能这就是大部分初级程序员日常开发的真实写照,在即使有测试人员验证的情况下,也会出现带Bug上线的现象,只不过是当时没有发现而已!因为是人写代码,就一定会有错误,即使是老码农 就程序Bug来讲,会包括产品PRD流程上的Bug.运营配置活动时候的Bug.研发开发时功能

  • SpringBoot的属性赋值@Value的用法说明

    目录 今天学习到了SpringBoot 的属性赋值@Value用法 先总结 例子 @Value的使用及注意事项 为什么使用 参数形式 前置条件(注意事项) 今天学习到了SpringBoot 的属性赋值@Value用法 先总结 @Value(" 张三 "):直接附在属性名上,在Bean初始化时,会赋初始值 @Value(" #{ 20 - 2 } "):可以用 #{ },里面可以写表达式,当然也可以直接 @Value(" #{ 18 } ") 或

  • Springboot中如何通过yml为实体类注入属性

    目录 1.编写实体类 2.编写yml文件 3.测试 4.结果 5.可以不用@ConfigurationProperties(prefix = “person”)注解方式 6.@ConfigurationProperties(prefix = “person”) 延伸:以后除了可以为实体类注入属性,还可以为配置类注入相关的配置信息 1.编写实体类 @Component @ConfigurationProperties(prefix = "person") public class Per

  • springboot配置文件属性变量引用方式${}和@@用法及区别说明

    目录 配置文件属性变量引用${}和@@用法 ${}常用于pom.xml @@方式常用于引用springboot非默认配置文件 配置文件中的“@”问题 springboot配置文件中的${…}和@…@ 起因 原因 解决 配置文件属性变量引用${}和@@用法 ${}和@@都是springboot引用属性变量的方式 具体区别与用法: ${}常用于pom.xml 和 src/main/resources/application.properties等默认配置文件的属性变量引用. 语法为:field_na

  • Spring Boot 如何正确读取配置文件属性

    目录 前言 @Value 示例代码 @ConfigurationProperties 示例代码 @EnableConfigurationProperties @ConfigurationPropertiesScan @PropertySource 示例代码 总结 前言 项目中经常会经常读取配置文件中的属性的值,Spring Boot提供了很多注解读取配置文件属性,那么如何正确使用呢? @Value @Value用来读取application.yml配置文件中属性的值. 示例代码 applicat

  • Spring源码解析 Bean属性填充

    目录 前言 属性填充 执行回调方法及后置处理器 前言 在上一篇文章中,我们分析了Spring中Bean的实例化过程,在结尾我们知道了虽然bean的实例化完成了,但是其中的属性还没有被注入,今天我们就接着来分析属性是如何被注入的. 属性填充 实例化完成后,回到上面第3条的doCreateBean方法中,看一下用BeanWrapper产生的原生对象,里面dao这个属性还是null值. 回归一下之前的代码,接下来要调用populateBean方法进行属性的填充: Object exposedObjec

  • Spring源码解析 Bean的实例化

    目录 前言 准备工作 实例化bean 1.AbstractBeanFactory 的 doGetBean方法 2.AbstractAutowireCapableBeanFactory 的 createBean方法 3.AbstractAutowireCapableBeanFactory 的 doCreateBean方法 4.AbstractAutowireCapableBeanFactory 的 createBeanInstance方法 5.AbstractAutowireCapableBean

  • Spring源码解析后置处理器梳理总结

    目录 前言 1.InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation()方法 2.SmartInstantiationAwareBeanPostProcessor的determineCandidateConstructors()方法 3.MergedBeanDefinitionPostProcessor的postProcessMergedBeanDefinition()方法 4.SmartInstantiationA

  • Spring源码解析之Bean的生命周期

    一.Bean的实例化概述 前一篇分析了BeanDefinition的封装过程,最终将beanName与BeanDefinition以一对一映射关系放到beanDefinitionMap容器中,这一篇重点分析如何利用bean的定义信息BeanDefinition实例化bean. 二.流程概览 其实bean的实例化过程比较复杂,中间细节很多,为了抓住重点,先将核心流程梳理出来,主要包含以下几个流程: step1: 通过反射创建实例: step2:给实例属性赋初始值: step3:如果Bean类实现B

  • Spring源码解析之Configuration

    一.@Configuration 1.1 未加@Configuration <!--logback-test.xml,配置不打印日志--> <?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <

  • Spring源码解析容器初始化构造方法

    目录 前言 构造方法 前言 Spring框架被广泛应用于我们的日常工作中,但是很长时间以来我都是只会使用,不懂它的作用原理.通过最近一段时间的阅读源码,个人发现通过阅读源码,能够帮助我们了解Spring的设计理念,并且对Java编程中的一些设计模式更加熟悉,所以记录一下自己对Spring源码的理解. 在开始进行源码学习前,首先再回顾一下三种Spring编程风格: 基于Schema,即通过xml标签的配置方式 基于Annotation的注解技术,使用@Component等注解配置bean 基于Ja

  • Spring源码解析之循环依赖的实现流程

    目录 前言 循环依赖实现流程 前言 上篇文章中我们分析完了Spring中Bean的实例化过程,但是没有对循环依赖的问题进行分析,这篇文章中我们来看一下spring是如何解决循环依赖的实现. 之前在讲spring的过程中,我们提到了一个spring的单例池singletonObjects,用于存放创建好的bean,也提到过这个Map也可以说是狭义上的spring容器. private final Map<String, Object> singletonObjects = new Concurr

  • Spring源码解析之BeanPostProcessor知识总结

    一.简介 BeanPostProcessor是Spring IOC容器给我们提供的一个扩展接口. 实例化Bean做前置处理.后置处理 二.接口定义 @Component public class BeanPost implements BeanPostProcessor { /** * 在每个bean创建之后的初始化方法之前调用 * @param bean 当前实例化的bean * @param beanName bean的名称 * @return 返回实例化的bean或者可以对对象进行再封装返

  • Spring源码解析之推断构造方法

    Spring推断构造方法 贴个测试代码直接开干,这只是个样例,其他情况自行分析 @Component public class OrderService { public OrderService() { System.out.println("无参构造方法"); } @Autowired(required = false) public OrderService(UserService userService) { System.out.println("一个参数的构造方法

  • Spring源码解析之编程式事务

    一.前言 在Spring中,事务有两种实现方式: 编程式事务管理: 编程式事务管理使用TransactionTemplate可实现更细粒度的事务控制.声明式事务管理: 基于Spring AOP实现.其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务. 声明式事务管理不需要入侵代码,通过@Transactional就可以进行事务操作,更快捷而且简单(尤其是配合spring boot自动配置,可以说是精简至极!),且大部分业务都可

随机推荐