近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~

先说明下场景,代码如下:

有如下接口:

public interface EmployeeService {
public EmployeeDto getEmployeeById(Long id);
}

同时有下述两个实现类 EmployeeServiceImpl和EmployeeServiceImpl1:

@Service("service")
public class EmployeeServiceImpl implements EmployeeService {
public EmployeeDto getEmployeeById(Long id) {
return new EmployeeDto();
}
} @Service("service1")
public class EmployeeServiceImpl1 implements EmployeeService {
public EmployeeDto getEmployeeById(Long id) {
return new EmployeeDto();
}
}

调用代码如下:

@Controller
@RequestMapping("/emplayee.do")
public class EmployeeInfoControl { @Autowired
EmployeeService employeeService; @RequestMapping(params = "method=showEmplayeeInfo")
public void showEmplayeeInfo(HttpServletRequest request, HttpServletResponse response, EmployeeDto dto) {
#略
}
}

在启动tomcat时报如下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeInfoControl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.test.service.EmployeeService com.test.controller.EmployeeInfoControl.employeeService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.test.service.EmployeeService] is defined: expected single matching bean but found 2: [service1, service2]

其实报错信息已经说得很明确了,在autoware时,由于有两个类实现了EmployeeService接口,所以Spring不知道应该绑定哪个实现类,所以抛出了如上错误。

这个时候就要用到@Qualifier注解了,qualifier的意思是合格者,通过这个标示,表明了哪个实现类才是我们所需要的,我们修改调用代码,添加@Qualifier注解,需要注意的是@Qualifier的参数名称必须为我们之前定义@Service注解的名称之一!

@Controller
@RequestMapping("/emplayee.do")
public class EmployeeInfoControl { @Autowired
@Qualifier("service")
EmployeeService employeeService; @RequestMapping(params = "method=showEmplayeeInfo")
public void showEmplayeeInfo(HttpServletRequest request, HttpServletResponse response, EmployeeDto dto) {
#略
}
}

装载自:https://www.cnblogs.com/smileLuckBoy/p/5801678.html

@Autowired 注解 是根据类型自动注入,如果<bean>中有多个该类型的bean,自动注入的时候会报BeanCreationException异常。所以通常通过@Autowired和@Qualifier一起配合着注解。

举例:例如bean中配了多个数据源。

<bean  class="org.springframework.jdbc.core.JdbcTemplate">
<qualifier value="dataSource1"/>
<property name="dataSource" ref="dataSource1" />
</bean> <bean class="org.springframework.jdbc.core.JdbcTemplate">
<qualifier value="dataSource2"/>
<property name="dataSource" ref="dataSource2" />
</bean>

在代码中用到的地方可以通过如下代码进行注解,这样防止冲突

@Autowired()
@Qualifier("dataSource1")
private JdbcTemplate jdbcTemplate;
@Autowired()
@Qualifier("dataSource2")
private JdbcTemplate jdbcTemplate;

Spring的注解@Qualifier的更多相关文章

  1. Spring的注解@Qualifier用法

    Spring的注解@Qualifier用法在Controller中需要注入service那么我的这个server有两个实现类如何区分开这两个impl呢?根据注入资源的注解不同实现的方式有一点小小的区别 ...

  2. Spring的注解@Qualifier(二十五)

    转载:https://www.cnblogs.com/smileLuckBoy/p/5801678.html 近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~ 先说明 ...

  3. Spring的注解@Qualifier小结

    有以下接口: public interface EmployeeService { public EmployeeDto getEmployeeById(Long id); } 有两个实现类: @Se ...

  4. Spring的注解@Qualifier注解

    @Qualifier注解了,qualifier的意思是合格者,通过这个标示,表明了哪个实现类才是我们所需要的,我们修改调用代码,添加@Qualifier注解,需要注意的是@Qualifier的参数名称 ...

  5. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  6. Spring JSR-250注解

    Java EE5中引入了“Java平台的公共注解(Common Annotations for the Java Platform)”,而且该公共注解从Java SE 6一开始就被包含其中. 2006 ...

  7. Spring 基于注解零配置开发

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...

  8. Spring中注解的使用详解

    一:@Rsource注解的使用规则 1.1.案例演示 Spring的主配置文件:applicationContext.xml(因为我这里将会讲到很多模块,所以我用一个主配置文件去加载各个模块的配置文件 ...

  9. Spring IOC的描述和Spring的注解(转)

    Spring常用的注解 本文系转载:转载网址: http://www.cnblogs.com/xdp-gacl/p/3495887.html http://ljhzzyx.blog.163.com/b ...

随机推荐

  1. 在GitHub,他们是怎么玩的? (转)

    文章来源: http://www.cocoachina.com/gamedev/misc/2014/0530/8616.html   Github.com,现在是全世界程序员,尤其是开源爱好者的乐园. ...

  2. react- 相关

    生命周期方法 组件的生命周期分成三个状态: Mounting:已插入真实 DOM Updating:正在被重新渲染 Unmounting:已移出真实 DOM React 为每个状态都提供了两种处理函数 ...

  3. 浅谈SQL Server中的事务日志(一)----事务日志的物理和逻辑构架

    简介 SQL Server中的事务日志无疑是SQL Server中最重要的部分之一.因为SQL SERVER利用事务日志来确保持久性(Durability)和事务回滚(Rollback).从而还部分确 ...

  4. [bootstrap]模态框总结

    <!--data-backdrop禁止点击空白处关闭模态框,也可以js设置,其他参数可参考官网模态框文档--> <div class="modal fade" i ...

  5. Selenium 元素查找

    1.尽量使用ID或者name去定位元素,如果这个元素没有ID或者Name,那么就是用它最近的父节点的ID或者Name去定位. 2.写自动化脚本不是一个人的事情,是一个团队的事情,合作能更好,更轻松得完 ...

  6. Do not set "root" as "NOPASSWD" in sudoers file

    cat /etc/sudoers root    ALL=(ALL)ALL: ALL do not change it to root    ALL=(ALL)NOPASSWD: ALL Since ...

  7. 拼接sql语句时拼接空字符串报sql错误

    先上代码(php): $id_card=""; $sql = "select * from people where id_card=".$id_card; 看 ...

  8. mysql 5.6 zip安装,启动失败,1067错误

    在使用mysql5.6 zip压缩包安装mysql过程中,启动过程,老是卡在1067启动错误上,翻看网上各种解决方案,卸载干净重装,重启,都不管用. 网上各种教程都是新建 my.ini mysql 配 ...

  9. 332. Reconstruct Itinerary (leetcode)

    1. build the graph and then dfs -- graph <String, List<String>>,  (the value is sorted a ...

  10. JS二维数组的写法以及注意事项

    最终数组:"line":[ { "Name":"WK_CT", "Sex":"CT", " ...