spring Annotation 源码分析1

 2019-11-02 21:07  阅读(927)
文章分类:Spring boot

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 elements = new LinkedList(); > Class targetClass = clazz; > > do \{ > LinkedList currElements = new LinkedList(); > 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); > \} ------- 来源:[http://ddrv.cn](http://ddrv.cn)

点赞(0)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> spring Annotation 源码分析1

相关推荐