为什么@Qualifier不能与@Autowired一起使用?

时间:2014-06-30 20:00:34

标签: spring spring-annotations

我有一个类:

@Service
@Qualifier("VeoExecutionService")
public class VeoExecutionService implements ExecutionService {

}

我在测试中使用它:

@Autowired
@Qualifier("VeoExecutionService")
private VeoExecutionService veoService;

但是在跑步的时候我得到了:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.trizic.service.veo.VeoExecutionService com.trizic.service.veo.VeoServiceImportAccountsTest.veoService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.trizic.service.veo.VeoExecutionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=VeoExecutionService)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
... 29 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.trizic.service.veo.VeoExecutionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=VeoExecutionService)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 31 more

3 个答案:

答案 0 :(得分:13)

我认为你应该使用类似的东西:

@Service("VeoExecutionService")
public class VeoExecutionService implements ExecutionService {

然后,您注入服务的地方:

@Autowired
@Qualifier("VeoExecutionService")
VeoExecutionService veoExecutionService;

请记住将<context:annotation-config />放入xml confing或@AnnotationDrivenConfig放入java配置。

请注意,<context:component-scan />(java配置中为@ComponentScan)也会激活<context:annotation-config />

答案 1 :(得分:1)

我同意gipinani的第一部分

@Service("VeoExecutionService")
public class VeoExecutionService implements ExecutionService {

但我认为注射部分必须这样写:

@Autowired
@Qualifier("VeoExecutionService")
Private ExecutionService veoExecutionService;

为了注入ExecutionService的VeoExecutionService实现

答案 2 :(得分:0)

试试这个:

@Autowired
@Qualifier("veoExecutionService")
private VeoExecutionService veoService;

v eoExecutionService”(小写 v