gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:36:55 24 4
gpt4 key购买 nike

我有一个类:

@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 {

然后,在您注入(inject)服务的地方:

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

记得输入<context:annotation-config />在你的 xml confing 或 @AnnotationDrivenConfig在 Java 配置中。

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

关于spring - 为什么@Qualifier 不能与@Autowired 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24498057/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com