最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring中的AutowireCandidateResolver的具體使用詳解

 更新時(shí)間:2020年04月23日 09:58:01   作者:程序員DMZ  
這篇文章主要介紹了Spring中的AutowireCandidateResolver的具體使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

接口定義

用于推斷一個(gè)特定的beanDefinition是否能作為指定依賴(lài)的候選者的策略接口

public interface AutowireCandidateResolver {
	// 默認(rèn)情況下直接根據(jù)bd中的定義返回,如果沒(méi)有進(jìn)行特殊配置的話為true
	default boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
		return bdHolder.getBeanDefinition().isAutowireCandidate();
	}
	
  // 指定的依賴(lài)是否是必要的
	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)依賴(lài),javax.inject.Qualifier注解
  // 默認(rèn)情況下返回false
	default boolean hasQualifier(DependencyDescriptor descriptor) {
		return false;
	}
	
  // QualifierAnnotationAutowireCandidateResolver做了實(shí)現(xiàn)
  // 獲取一個(gè)該依賴(lài)一個(gè)建議的值
	@Nullable
	default Object getSuggestedValue(DependencyDescriptor descriptor) {
		return null;
	}
	
  // 對(duì)某個(gè)依賴(lài)我們想要延遲注入,但是在創(chuàng)建Bean的過(guò)程中這個(gè)依賴(lài)又是必須的
  // 通過(guò)下面這個(gè)方法就能為延遲注入的依賴(lài)先生成一個(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è)類(lèi)

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不做為依賴(lài)進(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) {
    // 類(lèi)型上已經(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è)類(lèi)是最底層的子類(lèi),集成了所有的方法,并且額外提供了對(duì)依賴(lài)進(jìn)行延遲處理的能力

public class ContextAnnotationAutowireCandidateResolver extends QualifierAnnotationAutowireCandidateResolver {
	
  // 如果依賴(lài)需要進(jìn)行延遲處理,那么構(gòu)建一個(gè)代理對(duì)象先注入到bean中,不會(huì)直接去創(chuàng)建依賴(lài)對(duì)象
	@Override
	@Nullable
	public Object getLazyResolutionProxyIfNecessary(DependencyDescriptor descriptor, @Nullable String beanName) {
		return (isLazy(descriptor) ? buildLazyResolutionProxy(descriptor, beanName) : null);
	}
	
  // 依賴(lài)是否需要延遲處理
	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)行延遲注入依賴(lài),所有會(huì)向外暴露一個(gè)TargetSource,這個(gè)TargetSource的目標(biāo)為實(shí)際想要使用的對(duì)象,生成代理時(shí)會(huì)基于TargetSource進(jìn)行生成。在運(yùn)行期間(完成注入后)我們使用這個(gè)延遲處理的依賴(lài)時(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();
    // 如果依賴(lài)的類(lèi)型是一個(gè)接口,需要讓代理類(lèi)也實(shí)現(xiàn)這個(gè)接口
		if (dependencyType.isInterface()) {
			pf.addInterface(dependencyType);
		}
    // 生成代理
		return pf.getProxy(beanFactory.getBeanClassLoader());
	}

}

總結(jié)

  • SimpleAutowireCandidateResolver:?jiǎn)渭兊膶⒔涌谧兂闪丝蓪?shí)例化的類(lèi),方法實(shí)現(xiàn)跟接口保持一致
  • GenericTypeAwareAutowireCandidateResolver: 判斷泛型是否匹配,支持泛型依賴(lài)注入(From Spring4.0)
  • QualifierAnnotationAutowireCandidateResolver :處理 @Qualifier 和 @Value 注解
  • ContextAnnotationAutowireCandidateResolver :處理依賴(lài)級(jí)別的 @Lazy 注解,重寫(xiě)了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ò)程圖解

    這篇文章主要介紹了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)方法

    這篇文章主要介紹了Java中SpringSecurity密碼錯(cuò)誤5次鎖定用戶的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-03-03
  • Java查找不重復(fù)無(wú)序數(shù)組中是否存在兩個(gè)數(shù)字的和為某個(gè)值

    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幾種常用JSON庫(kù)性能比較詳解

    Java幾種常用JSON庫(kù)性能比較詳解

    這篇文章主要介紹了Java幾種常用JSON庫(kù)性能比較詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • Java并發(fā)編程必備之Synchronized關(guān)鍵字深入解析

    Java并發(fā)編程必備之Synchronized關(guān)鍵字深入解析

    本文我們深入探索了Java中的Synchronized關(guān)鍵字,包括其互斥性和可重入性的特性,文章詳細(xì)介紹了Synchronized的三種使用方式:修飾代碼塊、修飾普通方法和修飾靜態(tài)方法,感興趣的朋友一起看看吧
    2025-04-04
  • Springboot?多租戶SaaS搭建方案

    Springboot?多租戶SaaS搭建方案

    這篇文章主要介紹了Springboot?多租戶SaaS方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • Java數(shù)據(jù)結(jié)構(gòu)之線段樹(shù)詳解

    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
  • Java后端面試題最新整理

    Java后端面試題最新整理

    在本篇文章里小編給大家整理了一篇關(guān)于Java后端面試題最新整理內(nèi)容,需要的朋友們可以參考下。
    2020-12-12
  • Mybatis之解決collection一對(duì)多問(wèn)題(顯示的結(jié)果沒(méi)有整合到一起)

    Mybatis之解決collection一對(duì)多問(wèn)題(顯示的結(jié)果沒(méi)有整合到一起)

    這篇文章主要介紹了Mybatis之解決collection一對(duì)多問(wèn)題(顯示的結(jié)果沒(méi)有整合到一起),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • SpringBoot項(xiàng)目中使用AOP的方法

    SpringBoot項(xiàng)目中使用AOP的方法

    本篇文章主要介紹了SpringBoot項(xiàng)目中使用AOP的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02

最新評(píng)論

化隆| 百色市| 连山| 垦利县| 中西区| 广东省| 清水县| 滦平县| 平利县| 湾仔区| 汕尾市| 沈丘县| 许昌市| 鄱阳县| 成安县| 教育| 格尔木市| 米泉市| 邵东县| 密山市| 贵南县| 永登县| 德安县| 四平市| 红原县| 苍梧县| 黄骅市| 湘潭市| 尚志市| 会泽县| 肇源县| 全州县| 文水县| 岑溪市| 西和县| 松原市| 广元市| 赤城县| 文成县| 九龙县| 房产|