如何声明Spring bean autowire-candidate =" false"什么时候使用注释? [英] How to declare a Spring bean autowire-candidate="false" when using annotations?

查看:1451
本文介绍了如何声明Spring bean autowire-candidate =" false"什么时候使用注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 @ComponentScan @Component 来定义我的spring bean。我想要的是宣布其中一个bean为 autowire-candidate = false

I am using @ComponentScan and @Component to define my spring beans. What I would like is to declare one of these beans to be autowire-candidate=false.

这可以在xml中使用此属性来完成。是不是注释中的等价物?

This could be done with this attribute in xml. Isn't there the equivalent in annotations?

我想要这个的原因是因为我有2个相同接口的实现,我不想使用 @Qualifier

The reason I want this is because I have 2 implementations of the same interface and I don't want to use @Qualifier.

编辑:使用@Primary是一种有效的解决方法,但autowire-candidate在我看来似乎是一个有用的功能,有自己的语义。

Using @Primary is a valid work-around, but autowire-candidate seems to me like a useful feature with its own semantics.

谢谢

推荐答案

看起来Spring拒绝 autowire- candidate = false 概念,不再支持。没有带注释的模拟,所以 @Primary 是你注意到的最好的解决方法。

Looks like Spring refused autowire-candidate=false concept and it no longer supported. There is no analogue with annotations, so @Primary is the best work-around as you noticed.

另一种方式是使用自定义 org.springframework.beans.factory.support.AutowireCandidateResolver ,它在 DefaultListableBeanFactory 中用于逻辑从autowire候选人中排除不受欢迎的bean。在这种情况下,该技术将类似于 autowire-candidate = false SimpleAutowireCandidateResolver 中使用的技术。

Another way is to use custom org.springframework.beans.factory.support.AutowireCandidateResolver, which is used in DefaultListableBeanFactory, with logic that exclude undesirable beans from autowire candidates. In such case, the technology will be similar to that used for autowire-candidate=false in SimpleAutowireCandidateResolver.

这篇关于如何声明Spring bean autowire-candidate =" false"什么时候使用注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆