Spring 为什么';t@限定符是否与@Autowired一起工作?

Spring 为什么';t@限定符是否与@Autowired一起工作?,spring,spring-annotations,Spring,Spring Annotations,我有一门课,像: @Service @Qualifier("VeoExecutionService") public class VeoExecutionService implements ExecutionService { } 我在测试中使用它: @Autowired @Qualifier("VeoExecutionService") private VeoExecutionService veoService; 但跑步时我会: Caused by: org.springframew

我有一门课,像:

@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

我认为你应该使用这样的东西:

@Service("VeoExecutionService")
public class VeoExecutionService implements ExecutionService {
然后,在注入服务的地方:

@Autowired
@Qualifier("VeoExecutionService")
VeoExecutionService veoExecutionService;
请记住将
放在xml配置中,或将
@AnnotationDrivenConfig
放在java配置中


请注意,
@ComponentScan
在java配置中)也会激活

关于第一部分,我同意gipinani

@Service("VeoExecutionService")
public class VeoExecutionService implements ExecutionService {
但我认为注入部分必须这样写:

@Autowired
@Qualifier("VeoExecutionService")
Private ExecutionService veoExecutionService;
要注入ExecutionService的VeoExecutionService实现,请尝试以下操作:

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

veoExecutionService”(小写为v

因此问题与我在spring服务中使用@Transaction有关。根据Spring文档,它使用JDK动态代理(默认)或CGLIB。在JDK代理的情况下,它会感到困惑,因为服务还实现了一个接口。我找不到办法解决这个问题,但是在配置Spring使用CGLIB代理之后,一切都很好。这似乎是一个显而易见的问题,但他们经常让我们,您是否确保您的组件扫描实际上正在扫描您的服务的位置?我的问题通过将
放入xml配置文件中得到解决。我看不到任何配置,并且我怀疑是否首先加载了该服务。删除限定符并查看它是否有效。