spring Annotation 源码分析1

1、java双加锁机制:

 Constructor<?>[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);
  if (candidateConstructors == null) {
   synchronized (this.candidateConstructorsCache) {
    candidateConstructors = this.candidateConstructorsCache.get(beanClass);
    if (candidateConstructors == null) {

}}}

2、判断字段或者方法属性:Modifier.isStatic(field.getModifiers()) 、Modifier.isStatic(method.getModifiers())

 

3、获取字段或者方法的注解:

Annotation annotation = findAutowiredAnnotation(field);  \  Annotation annotation = findAutowiredAnnotation(method);

4、InjectionMetadata:管理注入元信息的内部类 userd by AutowiredAnnotationBeanPostProcessor and org.springframework.context.annotation.CommonAnnotationBeanPostProcessor  and  org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor

 

5、流程:

   (1): public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException {//容器对指定类进行自动依赖注入装配(autowiring)时,容器需要对Bean调用合适的构造方法创建实例对象,AutowiredAnnotationBeanPostProcessor为指定类选择相应的构造方法

(2):为指定的属性进行依赖注入

 public PropertyValues postProcessPropertyValues(
   PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {

  InjectionMetadata metadata = findAutowiringMetadata(bean.getClass());
  try {
   metadata.inject(bean, beanName, pvs);
  }
  catch (Throwable ex) {
   throw new BeanCreationException(beanName, “Injection of autowired dependencies failed”, ex);
  }
  return pvs;
 }

为对象进行注入

 public void processInjection(Object bean) throws BeansException {
  Class<?> clazz = bean.getClass();
  InjectionMetadata metadata = findAutowiringMetadata(clazz);
  try {
   metadata.inject(bean, null, null);
  }
  catch (Throwable ex) {
   throw new BeanCreationException(“Injection of autowired dependencies failed for class [” + clazz + “]”, ex);
  }
 }

 

//获取给定类的autowire相关注解元信息

 private InjectionMetadata findAutowiringMetadata(Class<?> clazz) {
  // Quick check on the concurrent map first, with minimal locking.
  InjectionMetadata metadata = this.injectionMetadataCache.get(clazz);
  if (metadata == null) {
   synchronized (this.injectionMetadataCache) {
    metadata = this.injectionMetadataCache.get(clazz);
    if (metadata == null) {
     metadata = buildAutowiringMetadata(clazz);
     this.injectionMetadataCache.put(clazz, metadata);
    }
   }
  }
  return metadata;
 }

 

 //解析给定类autowire相关注解元信息   

private InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
  LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
  Class<?> targetClass = clazz;

  do {
   LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>();
   for (Field field : targetClass.getDeclaredFields()) {
    Annotation annotation = findAutowiredAnnotation(field);
    if (annotation != null) {
     if (Modifier.isStatic(field.getModifiers())) {
      if (logger.isWarnEnabled()) {
       logger.warn(“Autowired annotation is not supported on static fields: ” + field);
      }
      continue;
     }
     boolean required = determineRequiredStatus(annotation);
     currElements.add(new AutowiredFieldElement(field, required));
    }
   }
   for (Method method : targetClass.getDeclaredMethods()) {
    Annotation annotation = findAutowiredAnnotation(method);
    if (annotation != 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);
      }
      continue;
     }
     if (method.getParameterTypes().length == 0) {
      if (logger.isWarnEnabled()) {
       logger.warn(“Autowired annotation should be used on methods with actual parameters: ” + method);
      }
     }
     boolean required = determineRequiredStatus(annotation);
     PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
     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);
 }

 

 

    原文作者:Spring Boot
    原文地址: https://blog.csdn.net/liuxianbing119/article/details/6721558
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞