Spring @Qualifier

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

有如下接口:

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) {
        #略
    }
}
时间: 2024-08-08 05:29:53

Spring @Qualifier的相关文章

spring @Qualifier 示例

@Qualifier 常出现在一个接口,多个实现类调用的情况下,在一些选择处理上,给出使用示例,体会此种妙处. package spring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Annota

spring @Qualifier注解

@Autowired是根据类型进行自动装配的.如果当spring上下文中存在不止一个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存在UserDao类型的bean,也会抛出BeanCreationException异常.我们可以使用@Qualifier配合@Autowired来解决这些问题.如下: ①可能存在多个UserDao实例 @Autowired @Qualifier("userServiceImpl") pub

Spring @qualifier @Required

B.@Qualifier org.springframework.beans.factory.annotation.Qualifier public @interface Qualifier This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring. It may also be used to annotate other custom annot

@Autowired @Resource @Qualifier的区别

参考博文: http://www.cnblogs.com/happyyang/articles/3553687.html http://blog.csdn.net/revent/article/details/49203619 http://blog.csdn.net/ad921012/article/details/49679745 spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@

Spring4实战学习笔记

<Spring4实战 第4版>2016年4月新出版的,之前的第三版看起来还是不错的,所以看到新版就直接买下来. 英文版源码地址:Spring in Action, Fourth Edition Covers Spring 4 1.IOC装配Bean 参考[Spring实战4 2.2],作者提倡无XML配置化. 1.1接口只有一个现实类 可以自动装配 public interface CompactDisc { void play(); } import org.springframework.

SSM框架指的是什么

SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成, 常作为数据源较简单的web项目的框架. 其中spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架. SpringMVC分离了控制器.模型对象.分派器以及处理程序对象的角色,这种分离让它们更容易进行定制. MyBatis是一个支持普通sql查询,存储过程和高级映射的优秀持久层框架. 下面分整理出SSM的系统学习教程,分别是Spring教程+

@Autowired 与@Resource的区别(详细)

参考博文: http://www.cnblogs.com/happyyang/articles/3553687.html http://blog.csdn.net/revent/article/details/49203619 http://blog.csdn.net/ad921012/article/details/49679745 spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@

Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析

URL:http://www.ulewo.com/user/10001/blog/273 我们在使用spring的时候经常会用到这些注解,那么这些注解到底有什么区别呢.我们先来看代码 同样分三层来看: Action 层: package com.ulewo.ioc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Co

Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解

原创整理不易,转载请注明出处:Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解 代码下载地址:http://www.zuidaima.com/share/1772661373422592.htm 本文介绍了使用Spring注解注入属性的方法.使用注解以前,注入属性通过类以及配置文件来实现.现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifi