【Spring源码分析】16-AutowiredAnnotationBeanPostProcessor

 2019-10-17 21:59  阅读(920)
文章分类:Spring boot

AutowiredAnnotationBeanPostProcessor继承了InstantiationAwareBeanPostProcessorAdapter间接实现了SmartInstantiationAwareBeanPostProcessor接口覆盖了determineCandidateConstructors()方法,此方法在实例化bean前选取一个构造方法数组,请参考bean创建的流程https://blog.csdn.net/shenchaohao12321/article/details/80441204https://blog.csdn.net/shenchaohao12321/article/details/80535708

先看determineCandidateConstructors()方法的前一小段是处理@Lookup注解的。

if (!this.lookupMethodsChecked.contains(beanName)) {
       try {
          ReflectionUtils.doWithMethods(beanClass, method -> {
             Lookup lookup = method.getAnnotation(Lookup.class);
             if (lookup != null) {
                Assert.state(beanFactory != null, "No BeanFactory available");
                LookupOverride override = new LookupOverride(method, lookup.value());
                try {
                   RootBeanDefinition mbd = (RootBeanDefinition) beanFactory.getMergedBeanDefinition(beanName);
                   mbd.getMethodOverrides().addOverride(override);
                }
                catch (NoSuchBeanDefinitionException ex) {
                   throw new BeanCreationException(beanName,
                      "Cannot apply @Lookup to beans without corresponding bean definition");
                }
             }
          });
       }
       catch (IllegalStateException ex) {
          throw new BeanCreationException(beanName, "Lookup method resolution failed", ex);
       }
       this.lookupMethodsChecked.add(beanName);
    }

将beanClass被@Lookup标记的方法封装到LookupOverride中,通过mbd.getMethodOverrides().addOverride(override)加入到BeanDefinition中,后面会在AbstractAutowireCapableBeanFactory的instantiateBean()方法中,会使用CglibSubclassingInstantiationStrategy的instantiate()方法创建bean对象。

@Override
    public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
       // Don't override the class with CGLIB if no overrides.
       if (!bd.hasMethodOverrides()) {
          Constructor<?> constructorToUse;
          synchronized (bd.constructorArgumentLock) {
             constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
             if (constructorToUse == null) {
                final Class<?> clazz = bd.getBeanClass();
                if (clazz.isInterface()) {
                   throw new BeanInstantiationException(clazz, "Specified class is an interface");
                }
                try {
                   if (System.getSecurityManager() != null) {
                      constructorToUse = AccessController.doPrivileged(
                            (PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);
                   }
                   else {
                      constructorToUse = clazz.getDeclaredConstructor();
                   }
                   bd.resolvedConstructorOrFactoryMethod = constructorToUse;
                }
                catch (Throwable ex) {
                   throw new BeanInstantiationException(clazz, "No default constructor found", ex);
                }
             }
          }
          return BeanUtils.instantiateClass(constructorToUse);
       }
       else {
          // Must generate CGLIB subclass.
          return instantiateWithMethodInjection(bd, beanName, owner);
       }
    }

可以看到如果!bd.hasMethodOverrides()则走instantiateWithMethodInjection()方法,这个方法内使用了CglibSubclassingInstantiationStrategy内部类CglibSubclassCreator来创建cglib的代理对象。CglibSubclassCreator使用的callback拦截器就包含LookupOverrideMethodInterceptor。

private static class LookupOverrideMethodInterceptor extends CglibIdentitySupport implements MethodInterceptor {

       private final BeanFactory owner;

       public LookupOverrideMethodInterceptor(RootBeanDefinition beanDefinition, BeanFactory owner) {
          super(beanDefinition);
          this.owner = owner;
       }

       @Override
       public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {
          // Cast is safe, as CallbackFilter filters are used selectively.
          LookupOverride lo = (LookupOverride) getBeanDefinition().getMethodOverrides().getOverride(method);
          Assert.state(lo != null, "LookupOverride not found");
          Object[] argsToUse = (args.length > 0 ? args : null);  // if no-arg, don't insist on args at all
          if (StringUtils.hasText(lo.getBeanName())) {
             return (argsToUse != null ? this.owner.getBean(lo.getBeanName(), argsToUse) :
                   this.owner.getBean(lo.getBeanName()));
          }
          else {
             return (argsToUse != null ? this.owner.getBean(method.getReturnType(), argsToUse) :
                   this.owner.getBean(method.getReturnType()));
          }
       }
    }

如果调用的方法是被标记的,如果配置了@Lookup的value属性则从BeanFactory中取出bean name为@Lookup的value属性的bean对象返回,否则根据方法的返回类型从BeanFactory中取出相应的bean对象。

以上就是@Lookup的处理过程。

determineCandidateConstructors()方法的剩余部分就是挑选出候选的构造方法,从所有声明的构造方法选取带有@Autowired注解的,但是@Autowired的required==true的只能有一个,如果还有无参构造方法,再加上无参构造方法。

一个bean实例创建之后但此时还没有调用populateBean()方法完成属性的填充时,会经历MergedBeanDefinitionPostProcessor的postProcessMergedBeanDefinition()方法。

@Override
    public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
       InjectionMetadata metadata = findAutowiringMetadata(beanName, beanType, null);
       metadata.checkConfigMembers(beanDefinition);
    }

private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) {
       // Fall back to class name as cache key, for backwards compatibility with custom callers.
       String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
       // Quick check on the concurrent map first, with minimal locking.
       InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
       if (InjectionMetadata.needsRefresh(metadata, clazz)) {
          synchronized (this.injectionMetadataCache) {
             metadata = this.injectionMetadataCache.get(cacheKey);
             if (InjectionMetadata.needsRefresh(metadata, clazz)) {
                if (metadata != null) {
                   metadata.clear(pvs);
                }
                metadata = buildAutowiringMetadata(clazz);
                this.injectionMetadataCache.put(cacheKey, metadata);
             }
          }
       }
       return metadata;
    }

injectionMetadataCache是一个Map对象,缓存beanName与InjectionMetadata。InjectionMetadata对象通过buildAutowiringMetadata()方法来创建的。

private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
       LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<>();
       Class<?> targetClass = clazz;
       do {
          final LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<>();
          ReflectionUtils.doWithLocalFields(targetClass, field -> {
             AnnotationAttributes ann = findAutowiredAnnotation(field);
             if (ann != null) {
                if (Modifier.isStatic(field.getModifiers())) {
                   if (logger.isWarnEnabled()) {
                      logger.warn("Autowired annotation is not supported on static fields: " + field);
                   }
                   return;
                }
                boolean required = determineRequiredStatus(ann);
                currElements.add(new AutowiredFieldElement(field, required));
             }
          });

          ReflectionUtils.doWithLocalMethods(targetClass, method -> {
             Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
             if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
                return;
             }
             AnnotationAttributes ann = findAutowiredAnnotation(bridgedMethod);
             if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                if (Modifier.isStatic(method.getModifiers())) {
                   if (logger.isWarnEnabled()) {
                      logger.warn("Autowired annotation is not supported on static methods: " + method);
                   }
                   return;
                }
                if (method.getParameterCount() == 0) {
                   if (logger.isWarnEnabled()) {
                      logger.warn("Autowired annotation should only be used on methods with parameters: " +
                            method);
                   }
                }
                boolean required = determineRequiredStatus(ann);
                PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
                currElements.add(new AutowiredMethodElement(method, required, pd));
             }
          });
          elements.addAll(0, currElements);
          targetClass = targetClass.getSuperclass();
       }
       while (targetClass != null && targetClass != Object.class);

       return new InjectionMetadata(clazz, elements);
    }

buildAutowiringMetadata()方法分为对字段与方法的@Autowired(@Value,@Inject)注解处理。将标记@Autowired的字段封装为AutowiredFieldElement对象,将标记@Autowired的方法并且此方法是字段的getter或writter方法封装到AutowiredMethodElement对象然后都加入到InjectionMetadata中,此过程是一个循环过程此类处理完会沿着父类继续向上处理。AutowiredFieldElement与AutowiredMethodElement都是InjectionMetadata.InjectedElement的子类,都覆盖了父类的inject()方法这个下面会介绍。

通过findAutowiringMetadata()方法得到了InjectionMetadata对象之后调用了给对象的checkConfigMembers()方法。这个方法就是将上面保存的InjectedElement对象筛选出那些不存在于beanDefinition中的赋值给checkedElements。

public void checkConfigMembers(RootBeanDefinition beanDefinition) {
       Set<InjectedElement> checkedElements = new LinkedHashSet<>(this.injectedElements.size());
       for (InjectedElement element : this.injectedElements) {
          Member member = element.getMember();
          if (!beanDefinition.isExternallyManagedConfigMember(member)) {
             beanDefinition.registerExternallyManagedConfigMember(member);
             checkedElements.add(element);
             if (logger.isDebugEnabled()) {
                logger.debug("Registered injected element on class [" + this.targetClass.getName() + "]: " + element);
             }
          }
       }
       this.checkedElements = checkedElements;
    }

到这为止就是postProcessMergedBeanDefinition()方法的全部实现逻辑并没有做什么实际事情,其实是为了后面的行为做铺垫,请看postProcessPropertyValues()方法,此方法是在属性填充阶段被调用的。

@Override
    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);
       }
       catch (BeanCreationException ex) {
          throw ex;
       }
       catch (Throwable ex) {
          throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex);
       }
       return pvs;
    }

首先调用 findAutowiringMetadata()方法从缓存中再次取到InjectionMetadata对象,调用inject()方法完成autowired注入。

public void inject(Object target, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
       Collection<InjectedElement> checkedElements = this.checkedElements;
       Collection<InjectedElement> elementsToIterate =
             (checkedElements != null ? checkedElements : this.injectedElements);
       if (!elementsToIterate.isEmpty()) {
          boolean debug = logger.isDebugEnabled();
          for (InjectedElement element : elementsToIterate) {
             if (debug) {
                logger.debug("Processing injected element of bean '" + beanName + "': " + element);
             }
             element.inject(target, beanName, pvs);
          }
       }
    }

因为之前已经把类@Autowired注解标记的字段或方法封装到了响应的InjectedElement子类中了,此时依次调用子类的inject()方法完成对象的注入,下面分别看一看这两种注入的实现。

AutowiredFieldElement,调用BeanFactory的resolveDependency()方法返回一个符合字段类型的bean对象设置到此bean上。

@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);
       }
    }

AutowiredMethodElement,类似通过setter方法完成属性的注入。

@Override
    protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
       if (checkPropertySkipping(pvs)) {
          return;
       }
       Method method = (Method) this.member;
       Object[] arguments;
       if (this.cached) {
          // Shortcut for avoiding synchronization...
          arguments = resolveCachedArguments(beanName);
       }
       else {
          Class<?>[] paramTypes = method.getParameterTypes();
          arguments = new Object[paramTypes.length];
          DependencyDescriptor[] descriptors = new DependencyDescriptor[paramTypes.length];
          Set<String> autowiredBeans = new LinkedHashSet<>(paramTypes.length);
          Assert.state(beanFactory != null, "No BeanFactory available");
          TypeConverter typeConverter = beanFactory.getTypeConverter();
          for (int i = 0; i < arguments.length; i++) {
             MethodParameter methodParam = new MethodParameter(method, i);
             DependencyDescriptor currDesc = new DependencyDescriptor(methodParam, this.required);
             currDesc.setContainingClass(bean.getClass());
             descriptors[i] = currDesc;
             try {
                Object arg = beanFactory.resolveDependency(currDesc, beanName, autowiredBeans, typeConverter);
                if (arg == null && !this.required) {
                   arguments = null;
                   break;
                }
                arguments[i] = arg;
             }
             catch (BeansException ex) {
                throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(methodParam), ex);
             }
          }
          synchronized (this) {
             if (!this.cached) {
                if (arguments != null) {
                   Object[] cachedMethodArguments = new Object[paramTypes.length];
                   System.arraycopy(descriptors, 0, cachedMethodArguments, 0, arguments.length);
                   registerDependentBeans(beanName, autowiredBeans);
                   if (autowiredBeans.size() == paramTypes.length) {
                      Iterator<String> it = autowiredBeans.iterator();
                      for (int i = 0; i < paramTypes.length; i++) {
                         String autowiredBeanName = it.next();
                         if (beanFactory.containsBean(autowiredBeanName) &&
                               beanFactory.isTypeMatch(autowiredBeanName, paramTypes[i])) {
                            cachedMethodArguments[i] = new ShortcutDependencyDescriptor(
                                  descriptors[i], autowiredBeanName, paramTypes[i]);
                         }
                      }
                   }
                   this.cachedMethodArguments = cachedMethodArguments;
                }
                else {
                   this.cachedMethodArguments = null;
                }
                this.cached = true;
             }
          }
       }
       if (arguments != null) {
          try {
             ReflectionUtils.makeAccessible(method);
             method.invoke(bean, arguments);
          }
          catch (InvocationTargetException ex){
             throw ex.getTargetException();
          }
       }
    }

来源:[]()

点赞(0)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> 【Spring源码分析】16-AutowiredAnnotationBeanPostProcessor

相关推荐