spring bean源码分析,

  spring bean源码分析,

  

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

  

前言

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

  

属性填充

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

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

  对象exposedObject=bean尝试{ populateBean(beanName,mbd,instance wrapper);exposedObject=initialize bean(bean name,exposed object,mbd);}看一下人口豆中的核心代码:

  for(BeanPostProcessor BP : getBeanPostProcessors()){ if(instantiationwarebeanpostprocessor的BP实例){ instantiationwarebeanpostprocessor IBP=(instantiationwarebeanpostprocessor)BP;IBP。postprocesspropertyvalues(PVS,filteredPds,bw.getWrappedInstance(),bean名称);if(PVS==null){ return;} }}这里通过getBeanPostProcessors方法获得当前注册的所有后置处理器,如果属于instantiationwarebeanpostprocessor类型,则调用它的后期处理属性值方法。通过遍历,可以知道当前春天中存在七个后置处理器:

  我们主要来看一下AutowiredAnnotationBeanPostProcessor,因为它负责对添加了@自动连线,@值等注解的属性进行依赖的填充。进入它的后期处理属性值方法:

  公共属性值postProcessPropertyValues(属性值PVS,PropertyDescriptor[] pds,对象bean,字符串beanName)抛出BeanCreationException { injection metadata metadata=find autowiringmetadata(bean name,bean.getClass(),PVS);试试{ metadata.inject(bean,beanName,PVS);} //异常处理代码省略.返回pvs}这里的注入元数据可以理解为要注入的属性的元数据,在它里面维护了一个收藏,来存放所有需要注入的bean:

  私有最终集合注入元素注入元素;进入findautowwiringmetadata方法:

  私有注入元数据查找autowiringmetadata(字符串bean名称,类?clazz,@ Nullable属性值PVS){ String cache key=(String utils。haslength(bean名称)?豆名: clazz。getname());InjectionMetadata元数据元数据元数据

  = 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属性填充内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

相关文章阅读

  • spring编程式事务处理,spring编程事务
  • spring编程式事务处理,spring编程事务,详解Spring学习之编程式事务管理
  • spring的核心功能模块有几个,列举一些重要的spring模块
  • spring的核心功能模块有几个,列举一些重要的spring模块,七个Spring核心模块详解
  • spring注解和springmvc的注解,SpringMVC常用注解
  • spring注解和springmvc的注解,SpringMVC常用注解,详解springmvc常用5种注解
  • spring实现ioc的四种方法,spring的ioc的三种实现方式
  • spring实现ioc的四种方法,spring的ioc的三种实现方式,简单实现Spring的IOC原理详解
  • spring事务失效问题分析及解决方案怎么做,spring 事务失效情况
  • spring事务失效问题分析及解决方案怎么做,spring 事务失效情况,Spring事务失效问题分析及解决方案
  • spring5.0新特性,spring4新特性
  • spring5.0新特性,spring4新特性,spring5新特性全面介绍
  • spring ioc以及aop原理,springmvc aop原理
  • spring ioc以及aop原理,springmvc aop原理,深入浅析Spring 的aop实现原理
  • Spring cloud网关,spring cloud zuul作用
  • 留言与评论(共有 条评论)
       
    验证码: