AbstractAutowireCapableBeanFactory類中的populateBean方法中的自動注入,有2種類型:一是根據(jù)名稱自動注入适揉,二是根據(jù)類型自動注入鸣剪,對應(yīng)的是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;
}