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

use*_*365 2 spring spring-annotations

我有一个类:

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

}
Run Code Online (Sandbox Code Playgroud)

我在测试中使用它:

@Autowired
@Qualifier("VeoExecutionService")
private VeoExecutionService veoService;
Run Code Online (Sandbox Code Playgroud)

但是当我跑步时我得到:

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
Run Code Online (Sandbox Code Playgroud)

gip*_*ani 13

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

@Service("VeoExecutionService")
public class VeoExecutionService implements ExecutionService {
Run Code Online (Sandbox Code Playgroud)

然后,你注入你的服务:

@Autowired
@Qualifier("VeoExecutionService")
VeoExecutionService veoExecutionService;
Run Code Online (Sandbox Code Playgroud)

记得放入<context:annotation-config />你的xml confing或@AnnotationDrivenConfigjava配置.

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