博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring处理自动装配歧义
阅读量:4090 次
发布时间:2019-05-25

本文共 3664 字,大约阅读时间需要 12 分钟。

处理自动装配歧义 由发表在

自动装配中冲突

自动装配(autowiring)要求bean的匹配具备唯一性,否则就会产生歧义,从而抛出异常。

例如,如果我们给QunarBookingServiceCtripBookingService都标注上@Component,则应用上下文中会有两个BookingService实例。

QunarBookingService.java

@Componentpackage com.tianmaying.iocdemo;public class QunarBookingService implements BookingService {    public void bookFlight() {        System.out.println("book fight by Qunar!");    }}

CtripBookingService.java

@Componentpackage com.tianmaying.iocdemo;public class CtripBookingService implements BookingService {    public void bookFlight() {        System.out.println("book fight by Ctrip!");    }}

SmartBoss的代码中用@Autowired标准的bookingService属性,那么当Spring进行装配时,注入哪一个实现BookingService接口的Bean就会面临多个选择。Spring就会抛出异常NoUniqueBeanDefinitionException

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.tianmaying.dao.UserDao com.tianmaying.service.UserService.userDao; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.tianmaying.dao.UserDao] is defined: expected single matching bean but found 2: userDaoImpl,userMemoryRepository    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]    ... 43 common frames omittedCaused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.tianmaying.dao.UserDao] is defined: expected single matching bean but found 2: userDaoImpl,userMemoryRepository    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1126) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]    ... 45 common frames omitted

通过优先级

在定义bean时,可以通过@Primary指定一个优先级高的bean来消除自动装配过程中遇到的歧义问题。

比如,如果SmartBoos希望使用QunarBookingService的实例,那么我们可以给其增加一个@Primary标注。

QunarBookingService.java

@Component@Primarypackage com.tianmaying.iocdemo;public class QunarBookingService implements BookingService {    public void bookFlight() {        System.out.println("book fight by Qunar!");    }}

@Primary作用在类上适用于自动装配。也可以通过XML配置或者Java配置的方式实现@Primary标注的功能。

如果使用基于XML文件的显式配置,则可以如下定义:

如果使用基于XML文件的显式配置,则可以如下定义:

@Bean@Primarypublic QunarBookingService qunarBookingService() {  return new QunarBookingService();}

通过Qualifier

@Qualifier注解可以跟@Autowired@Inject一起使用,指定被注入的bean的ID。

每个Bean都有一个默认的Qualifier,内容与Bean的ID相同。

比如,我们也可以这样修改SmartBoss的代码:

package com.tianmaying.iocdemo;...@Componentpublic class SmartBoss {    private BookingService bookingService;    @Autowired    @Qualifier('qunarBookingService')    public void setBookingService(BookingService bookingService) {        this.bookingService = bookingService;    }    ...}

这样就告诉Spring给SmartBoss注入Qualifer为qunarBookingService的Bean。

我们也可以自定义Bean的Qualifier。在使用自定义的@Qualifier值时,最好选择一个含义准确意义明确的词。

更多文章请访问

你可能感兴趣的文章
iOS开发 支付之银联支付集成
查看>>
iOS开发支付集成之微信支付
查看>>
浅谈JavaScript--声明提升
查看>>
React非嵌套组件通信
查看>>
Websocket 使用指南
查看>>
浏览器兼容性问题解决方案 · 总结
查看>>
一个很棒的Flutter学习资源列表
查看>>
为什么你应该放弃React老的Context API用新的Context API
查看>>
Flutter 布局控件完结篇
查看>>
Koa2初体验
查看>>
Koa 2 初体验(二)
查看>>
Koa2框架原理解析和实现
查看>>
vue源码系列文章good
查看>>
你不知道的Virtual DOM
查看>>
VUE面试题总结
查看>>
写好JavaScript条件语句的5条守则
查看>>
原生JS中DOM节点相关API合集
查看>>
【TINY4412】U-BOOT移植笔记:(7)SDRAM驱动
查看>>
【TINY4412】U-BOOT移植笔记:(12)BEEP驱动
查看>>
单链表的修改和删除
查看>>