Spring應用拋出NoUniqueBeanDefinitionException異常的解決方案
前言
我們在開發Spring應用時可能會不小心註入兩個相同類型的Bean,比如實現瞭兩個相同Service接口的類,示例偽代碼如下:
interface SampleService { String getName(); } class ServiceA implements SampleService{ String getName(){ return "john"; } } class ServiceB implements SampleService{ String getName(){ return "wonder"; } }
這時候我們用SampleService接口註入
@Autowired SampleService 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 @Component class ServiceA implements SampleService{ String getName(){ return "john"; } }
註解在有@Bean註解的方法上:
@Bean @Primary SampleService sampleService(){ return new ServiceA(); }
2.還是采用Xml配置中的第三或者第四種解決方案,隻是采用第四種方案的話還是需要重新擴展AnnotationAwareOrderComparator
以上就是Spring應用拋出NoUniqueBeanDefinitionException異常的解決方案的詳細內容,更多關於Spring 拋出異常的解決的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- Spring 父類變量註入失敗的解決
- 老生常談spring的事務傳播機制
- Spring筆記-@Order註解和Ordered接口解析
- Spring事務註解@Transactional失效的八種場景分析
- SpringBean依賴和三級緩存的案例講解