Spring中的AutowireCandidateResolver的具體使用詳解
接口定義
用于推斷一個(gè)特定的beanDefinition是否能作為指定依賴的候選者的策略接口
public interface AutowireCandidateResolver {
// 默認(rèn)情況下直接根據(jù)bd中的定義返回,如果沒(méi)有進(jìn)行特殊配置的話為true
default boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
return bdHolder.getBeanDefinition().isAutowireCandidate();
}
// 指定的依賴是否是必要的
default boolean isRequired(DependencyDescriptor descriptor) {
return descriptor.isRequired();
}
// QualifierAnnotationAutowireCandidateResolver做了實(shí)現(xiàn),判斷是否有@Qualifier注解
// 一共有兩種注解:
// 1.Spring內(nèi)置的@Qualifier注解,org.springframework.beans.factory.annotation.Qualifier
// 2.添加了JSR-330相關(guān)依賴,javax.inject.Qualifier注解
// 默認(rèn)情況下返回false
default boolean hasQualifier(DependencyDescriptor descriptor) {
return false;
}
// QualifierAnnotationAutowireCandidateResolver做了實(shí)現(xiàn)
// 獲取一個(gè)該依賴一個(gè)建議的值
@Nullable
default Object getSuggestedValue(DependencyDescriptor descriptor) {
return null;
}
// 對(duì)某個(gè)依賴我們想要延遲注入,但是在創(chuàng)建Bean的過(guò)程中這個(gè)依賴又是必須的
// 通過(guò)下面這個(gè)方法就能為延遲注入的依賴先生成一個(gè)代理注入到bean中
@Nullable
default Object getLazyResolutionProxyIfNecessary(DependencyDescriptor descriptor, @Nullable String beanName) {
return null;
}
}
繼承關(guān)系

可以看到繼承關(guān)系都是單層的,我們就一個(gè)一個(gè)往下看
SimpleAutowireCandidateResolver
相比于接口沒(méi)有什么區(qū)別,實(shí)現(xiàn)也就是父接口中的默認(rèn)實(shí)現(xiàn),一般也不會(huì)使用這個(gè)類
public class SimpleAutowireCandidateResolver implements AutowireCandidateResolver {
@Override
public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
return bdHolder.getBeanDefinition().isAutowireCandidate();
}
@Override
public boolean isRequired(DependencyDescriptor descriptor) {
return descriptor.isRequired();
}
@Override
@Nullable
public Object getSuggestedValue(DependencyDescriptor descriptor) {
return null;
}
@Override
@Nullable
public Object getLazyResolutionProxyIfNecessary(DependencyDescriptor descriptor, @Nullable String beanName) {
return null;
}
}
GenericTypeAwareAutowireCandidateResolver
額外增加了對(duì)泛型的處理能力
public class GenericTypeAwareAutowireCandidateResolver extends SimpleAutowireCandidateResolver
implements BeanFactoryAware {
@Nullable
private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@Nullable
protected final BeanFactory getBeanFactory() {
return this.beanFactory;
}
@Override
public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
if (!super.isAutowireCandidate(bdHolder, descriptor)) {
// 如果bd中已經(jīng)配置了這個(gè)bean不做為依賴進(jìn)行注入的話,直接返回false
return false;
}
// 檢查泛型是否匹配
return checkGenericTypeMatch(bdHolder, descriptor);
}
}
QualifierAnnotationAutowireCandidateResolver
增加了對(duì)@Qualifier注解以及@Value注解的處理能力
public class QualifierAnnotationAutowireCandidateResolver extends GenericTypeAwareAutowireCandidateResolver {
private final Set<Class<? extends Annotation>> qualifierTypes = new LinkedHashSet<>(2);
// @Value注解
private Class<? extends Annotation> valueAnnotationType = Value.class;
// @Qualifier注解
@SuppressWarnings("unchecked")
public QualifierAnnotationAutowireCandidateResolver() {
this.qualifierTypes.add(Qualifier.class);
try {
this.qualifierTypes.add((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Qualifier",
QualifierAnnotationAutowireCandidateResolver.class.getClassLoader()));
}
catch (ClassNotFoundException ex) {
// JSR-330 API not available - simply skip.
}
}
// .......
@Override
public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
// 類型上已經(jīng)匹配了
boolean match = super.isAutowireCandidate(bdHolder, descriptor);
if (match) {
// 還需要判斷是否滿足@Qualifier注解的要求
match = checkQualifiers(bdHolder, descriptor.getAnnotations());
if (match) {
MethodParameter methodParam = descriptor.getMethodParameter();
if (methodParam != null) {
Method method = methodParam.getMethod();
if (method == null || void.class == method.getReturnType()) {
match = checkQualifiers(bdHolder, methodParam.getMethodAnnotations());
}
}
}
}
return match;
}
// .....
// 是否是@Qualifier注解
protected boolean isQualifier(Class<? extends Annotation> annotationType) {
for (Class<? extends Annotation> qualifierType : this.qualifierTypes) {
if (annotationType.equals(qualifierType) || annotationType.isAnnotationPresent(qualifierType)) {
return true;
}
}
return false;
}
@Override
@Nullable
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;
}
// 查找@Value注解
@Nullable
protected Object findValue(Annotation[] annotationsToSearch) {
if (annotationsToSearch.length > 0) {
AnnotationAttributes attr = AnnotatedElementUtils.getMergedAnnotationAttributes(
AnnotatedElementUtils.forAnnotations(annotationsToSearch), this.valueAnnotationType);
if (attr != null) {
return extractValue(attr);
}
}
return null;
}
// 獲取@Value注解中的值
protected Object extractValue(AnnotationAttributes attr) {
Object value = attr.get(AnnotationUtils.VALUE);
if (value == null) {
throw new IllegalStateException("Value annotation must have a value attribute");
}
return value;
}
}
ContextAnnotationAutowireCandidateResolver
這個(gè)類是最底層的子類,集成了所有的方法,并且額外提供了對(duì)依賴進(jìn)行延遲處理的能力
public class ContextAnnotationAutowireCandidateResolver extends QualifierAnnotationAutowireCandidateResolver {
// 如果依賴需要進(jìn)行延遲處理,那么構(gòu)建一個(gè)代理對(duì)象先注入到bean中,不會(huì)直接去創(chuàng)建依賴對(duì)象
@Override
@Nullable
public Object getLazyResolutionProxyIfNecessary(DependencyDescriptor descriptor, @Nullable String beanName) {
return (isLazy(descriptor) ? buildLazyResolutionProxy(descriptor, beanName) : null);
}
// 依賴是否需要延遲處理
protected boolean isLazy(DependencyDescriptor descriptor) {
for (Annotation ann : descriptor.getAnnotations()) {
Lazy lazy = AnnotationUtils.getAnnotation(ann, Lazy.class);
if (lazy != null && lazy.value()) {
return true;
}
}
MethodParameter methodParam = descriptor.getMethodParameter();
if (methodParam != null) {
Method method = methodParam.getMethod();
if (method == null || void.class == method.getReturnType()) {
Lazy lazy = AnnotationUtils.getAnnotation(methodParam.getAnnotatedElement(), Lazy.class);
if (lazy != null && lazy.value()) {
return true;
}
}
}
return false;
}
// 構(gòu)建延遲處理的代理對(duì)象
protected Object buildLazyResolutionProxy(final DependencyDescriptor descriptor, final @Nullable String beanName) {
final DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) getBeanFactory();
// 創(chuàng)建了一個(gè)TargetSource
TargetSource ts = new TargetSource() {
@Override
public Class<?> getTargetClass() {
return descriptor.getDependencyType();
}
@Override
public boolean isStatic() {
return false;
}
// target是我們實(shí)際想要使用的對(duì)象,如果不進(jìn)行延遲處理,那么注入到bean中的應(yīng)該就是這個(gè)對(duì)象
// 但是因?yàn)橐M(jìn)行延遲注入依賴,所有會(huì)向外暴露一個(gè)TargetSource,這個(gè)TargetSource的目標(biāo)為實(shí)際想要使用的對(duì)象,生成代理時(shí)會(huì)基于TargetSource進(jìn)行生成。在運(yùn)行期間(完成注入后)我們使用這個(gè)延遲處理的依賴時(shí)實(shí)際調(diào)用的會(huì)是target中的方法。
@Override
public Object getTarget() {
Object target = beanFactory.doResolveDependency(descriptor, beanName, null, null);
if (target == null) {
Class<?> type = getTargetClass();
if (Map.class == type) {
return Collections.emptyMap();
}
else if (List.class == type) {
return Collections.emptyList();
}
else if (Set.class == type || Collection.class == type) {
return Collections.emptySet();
}
throw new NoSuchBeanDefinitionException(descriptor.getResolvableType(),
"Optional dependency not present for lazy injection point");
}
return target;
}
@Override
public void releaseTarget(Object target) {
}
};
// 使用ProxyFactory,給TargetSource生成一個(gè)代理
ProxyFactory pf = new ProxyFactory();
pf.setTargetSource(ts);
Class<?> dependencyType = descriptor.getDependencyType();
// 如果依賴的類型是一個(gè)接口,需要讓代理類也實(shí)現(xiàn)這個(gè)接口
if (dependencyType.isInterface()) {
pf.addInterface(dependencyType);
}
// 生成代理
return pf.getProxy(beanFactory.getBeanClassLoader());
}
}
總結(jié)
- SimpleAutowireCandidateResolver:?jiǎn)渭兊膶⒔涌谧兂闪丝蓪?shí)例化的類,方法實(shí)現(xiàn)跟接口保持一致
- GenericTypeAwareAutowireCandidateResolver: 判斷泛型是否匹配,支持泛型依賴注入(From Spring4.0)
- QualifierAnnotationAutowireCandidateResolver :處理 @Qualifier 和 @Value 注解
- ContextAnnotationAutowireCandidateResolver :處理依賴級(jí)別的 @Lazy 注解,重寫了getLazyResolutionProxyIfNecessary 方法。
到此這篇關(guān)于Spring中的AutowireCandidateResolver的具體使用詳解的文章就介紹到這了,更多相關(guān)Spring AutowireCandidateResolver內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jmeter多用戶并發(fā)壓力測(cè)試過(guò)程圖解
這篇文章主要介紹了Jmeter多用戶并發(fā)壓力測(cè)試過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Java中SpringSecurity密碼錯(cuò)誤5次鎖定用戶的實(shí)現(xiàn)方法
這篇文章主要介紹了Java中SpringSecurity密碼錯(cuò)誤5次鎖定用戶的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Java查找不重復(fù)無(wú)序數(shù)組中是否存在兩個(gè)數(shù)字的和為某個(gè)值
今天小編就為大家分享一篇關(guān)于Java查找不重復(fù)無(wú)序數(shù)組中是否存在兩個(gè)數(shù)字的和為某個(gè)值,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
Java并發(fā)編程必備之Synchronized關(guān)鍵字深入解析
本文我們深入探索了Java中的Synchronized關(guān)鍵字,包括其互斥性和可重入性的特性,文章詳細(xì)介紹了Synchronized的三種使用方式:修飾代碼塊、修飾普通方法和修飾靜態(tài)方法,感興趣的朋友一起看看吧2025-04-04
Java數(shù)據(jù)結(jié)構(gòu)之線段樹(shù)詳解
線段樹(shù)是一種二叉搜索樹(shù),與區(qū)間樹(shù)相似,它將一個(gè)區(qū)間劃分成一些單元區(qū)間,每個(gè)單元區(qū)間對(duì)應(yīng)線段樹(shù)中的一個(gè)葉結(jié)點(diǎn)。本文將介紹線段樹(shù)的Java實(shí)現(xiàn)代碼,需要的可以參考一下2022-01-01
Mybatis之解決collection一對(duì)多問(wèn)題(顯示的結(jié)果沒(méi)有整合到一起)
這篇文章主要介紹了Mybatis之解決collection一對(duì)多問(wèn)題(顯示的結(jié)果沒(méi)有整合到一起),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03

