最近看到了个面试题好像就这么问的
一个接口有2个不同的实现,如何Autowire某一个指定的实现?
先用代码理解下提干
一个接口
两个实现
然后调用代码
跑下代码直接报错了
因为该接口不知道该映射到哪个实现类
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]
这个时候就要用到@Qualifier注解了,qualifier的意思是合格者,通过这个标示,表明了哪个实现类才是我们所需要的,我们修改调用代码,添加@Qualifier注解,需要注意的是@Qualifier的参数名称必须为我们之前定义@Service注解的名称之一!
这个就是我们要的答案
感觉有帮助的可以关注下,我也一直加油的