spring中的数组依赖注入?
有没有办法使用依赖注入来注入 Spring 中特定接口的所有可用实现?
这与 针对 .NET 的要求是一样的。
虽然我的目标是为此使用 @Autowired:
public class Foo{
@Autowired
private IDataCollector[] collectors;
}
这是否受支持,这是否需要黑客攻击,或者我应该最好使用另一个组件,其中 IDataCollector 的所有实现都自行注册并使用该访问器组件进行自动连接而不是数组注入?
我能想到为什么这可能不会默认实现的一个原因可能是,它还会在不合适的地方注入可能的模拟实现。尽管我仍然感兴趣这是否可能。 :)
is there a way to use dependency injection to inject all available implementations of a specific interface in spring?
This is kind of the same thing as asked here for .NET.
Though my aim is to use @Autowired for this:
public class Foo{
@Autowired
private IDataCollector[] collectors;
}
Is this supported, would this require hacking or should I preferably use another component where all implementations of IDataCollector register themselve and use that accessor component with autowired instead of the array injection?
A reason I can think of why this may not be implemented per default may be, that it would also inject possible mock implementations where inappropriate. Though I'm still interested wether this is possible or not. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
你可以注入一个列表,Spring 会为你转换它:
<util:list id="collectors">
<value>[email protected]</value>
<value>[email protected]</value>
</util:list>
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您的示例应该可以正常工作,
List
也应该可以正常工作。在询问之前您是否尝试过,但发现不起作用?https://docs。 spring.io/spring/docs/current/spring-framework-reference/core.html#beans-autowired-annotation
Your example should work fine, as should
List<IDataCollector>
. Did you give it a try before asking, and found it didn't work?https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-autowired-annotation