国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Spring應用拋出NoUniqueBeanDefinitionException異常的解決方案

瀏覽:3日期:2023-07-08 16:43:54
目錄前言解決方案前言

我們在開發Spring應用時可能會不小心注入兩個相同類型的Bean,比如實現了兩個相同Service接口的類,示例偽代碼如下:

interface SampleService { String getName();}class ServiceA implements SampleService{ String getName(){ return 'john'; }}class ServiceB implements SampleService{ String getName(){ return 'wonder'; }}

這時候我們用SampleService接口注入

@AutowiredSampleService sampleService;

啟動應用后,Spring就會優雅地提示如下錯誤:

Exception in thread 'main' org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ’com.john.primary.SampleService’ available: expected single matching bean but found 2: ServiceA,ServiceB

但是我們不想報錯且想獲取其中某一個Bean,這時候我們該怎么辦呢?

解決方案

既然包含了兩個相同類型的Bean,通常來說我們只要把其中一個Bean不注入就好,那如果我們想保留這兩個相同類型的Bean,但是又想讓SampleService正常注入呢?

如果我們是用早期Spring的Xml配置Bean時,可以使用如下兩種方式解決:

1.那么我們可以在其中一個Bean配置里加上autowire-candidate='false'

<bean id='serviceA' class='com.john.primary.ServiceA' /><bean id='serviceB' class='com.john.primary.ServiceB' autowire-candidate='false' />

2.或者在其中一個Bean配置里加上primary='true':

<bean id='serviceA' class='com.john.primary.ServiceA' primary='true'/><bean id='serviceB' class='com.john.primary.ServiceB' />

3.采用javax.annotation.Priority注解

這種方式需要我們在BeanFactory里加上dependencyComparator,示例代碼如下:

DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)context.getBeanFactory();//@Priority注解比較beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);SampleService sampleService= context.getBean(SampleService.class);

4.實現注解Order或者實現org.springframework.core.Ordered接口

public class ServiceA implements SampleService,Ordered { @Override public int getOrder() { return 0; } @Override public String toString() { return 'ServiceA{}'; }}

這種方式需要我們重寫AnnotationAwareOrderComparator的getPriority方法,示例代碼如下:

public class PriorityOrderComparator extends AnnotationAwareOrderComparator { /** * Shared default instance of {@code PriorityOrderComparator}. */ public static final PriorityOrderComparator INSTANCE = new PriorityOrderComparator(); @Override public Integer getPriority(Object obj) { //先獲取Priority Integer order = super.getPriority(obj); if(order == null)//獲取Order注解或者Ordered接口返回值return super.findOrder(obj); return order; }}

我們還可以使用目前流行的注解方式來實現,Spring文檔中也提到過:

Because autowiring by type may lead to multiple candidates, it is often necessary to have more control over the selection process. One way to accomplish this is with Spring’s @Primary annotation. @Primary indicates that a particular bean should be given preference when multiple beans are candidates to be autowired to a single-valued dependency. If exactly one primary bean exists among the candidates, it becomes the autowired value.

那么可以使用如下方式:

1.@Primary注解:

該注解可以標注在類上或者方法上,示例如下:

@Primary@Componentclass ServiceA implements SampleService{ String getName(){ return 'john'; }}

注解在有@Bean注解的方法上:

@Bean@PrimarySampleService sampleService(){ return new ServiceA();}

2.還是采用Xml配置中的第三或者第四種解決方案,只是采用第四種方案的話還是需要重新擴展AnnotationAwareOrderComparator

以上就是Spring應用拋出NoUniqueBeanDefinitionException異常的解決方案的詳細內容,更多關于Spring 拋出異常的解決的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 仁寿县| 秦安县| 凌海市| 公主岭市| 平邑县| 平乡县| 阜康市| 泽州县| 清镇市| 宁德市| 准格尔旗| 旬邑县| 凌云县| 平和县| 邛崃市| 多伦县| 兰坪| 内丘县| 龙胜| 曲水县| 江阴市| 商水县| 巩义市| 南召县| 襄城县| 安远县| 穆棱市| 桦南县| 洮南市| 宜川县| 扶余县| 平山县| 万宁市| 丁青县| 昭通市| 兴山县| 乌拉特中旗| 临潭县| 绥中县| 济南市| 渭南市|