美文网首页
2022-01-18 Spring中的@Value注解中的占位

2022-01-18 Spring中的@Value注解中的占位

作者: 归去来ming | 来源:发表于2022-01-18 00:30 被阅读0次

AbstractAutowireCapableBeanFactory类中的populateBean方法中的自动注入,有2种类型:一是根据名称自动注入,二是根据类型自动注入,对应的是2个方法:autowireByName, autowireByType。
@Value注解的处理逻辑在autowireByType方法中:

// resolveDependency
Object autowiredArgument = resolveDependency(desc, beanName, autowiredBeanNames, converter);

// 进入resolveDependency方法,它是一个抽象方法,进入DefaultListableBeanFactory类,真正的处理逻辑在doResolveDependency方法中。
图片.png 图片.png 图片.png

进入getSuggestedValue方法:


图片.png

来到QualifierAnnotationAutowireCandidateResolver类,看到findValue方法中使用了valueAnnotationType ,即Value.class

    private Class<? extends Annotation> valueAnnotationType = Value.class;
    // ……
    @Override
    public Object getSuggestedValue(DependencyDescriptor descriptor) {
        Object value = findValue(descriptor.getAnnotations());
        if (value == null) {
            MethodParameter methodParam = descriptor.getMethodParameter();
            if (methodParam != null) {
                value = findValue(methodParam.getMethodAnnotations());
            }
        }
        return value;
    }

    /**
     * Determine a suggested value from any of the given candidate annotations.
     */
    protected Object findValue(Annotation[] annotationsToSearch) {
        if (annotationsToSearch.length > 0) {   // qualifier annotations have to be local
            AnnotationAttributes attr = AnnotatedElementUtils.getMergedAnnotationAttributes(
                    AnnotatedElementUtils.forAnnotations(annotationsToSearch), this.valueAnnotationType);
            if (attr != null) {
                return extractValue(attr);
            }
        }
        return null;
    }

相关文章

网友评论

      本文标题:2022-01-18 Spring中的@Value注解中的占位

      本文链接:https://www.haomeiwen.com/subject/ndghhrtx.html