如果在自动装配中使用(required = false),则@Autowired与@Qualifier不起作用

时间:2019-07-12 11:18:14

标签: java spring autowired

我正在尝试将@Autowired与@Qualifier一起使用,在这样的Autowired批注中具有required = false

@Autowired(required=false)
@Qualifier("myValidator")
private MyValidatorClass validator;

如果我没有在上下文xml文件中初始化此特定的bean,则它不应给出错误。

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myValidator' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:775)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1221)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeanByName(AbstractAutowireCapableBeanFactory.java:452)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:526)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:636)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:180)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:321)
... 39 more

根据我的业务需求,我希望在属性上同时添加两个注释。我是春季新手,请帮助我解决此问题。

0 个答案:

没有答案