春季自动接线的参数化集合 [英] Spring Autowiring of Parameterized Collection

查看:90
本文介绍了春季自动接线的参数化集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,感谢您的提前帮助.

Hello everyone and thanks for the help in advance.

我遇到了一个问题,即Spring无法自动装配ArrayBlockingQueue类型的参数化成员变量.

I am having a problem where Spring cannot autowire a parametirized member variable of type ArrayBlockingQueue.

这是Java代码:

@Controller
public class SomeController
{
    @Autowired
    private ArrayBlockingQueue<SomeCustomType> myQueue;
}

并在spring配置xml中:

and in the spring configuration xml:

<bean id="myQueue" class="java.util.concurrent.ArrayBlockingQueue">
    <constructor-arg value="10"/>
</bean>

为ArrayBlockingQueue指定类型(SomeCustomType)似乎会使spring迷惑,因为spring无法找到匹配项并且不执行自动装配.

Specifying the type (SomeCustomType) for the ArrayBlockingQueue seems to confuse spring which fails to find a match and does not perform the autowiring.

关于如何使它起作用的任何想法?我知道我可以创建自己的未参数化的包装器类(在ArrayBlockingQueue周围),但是如果有更好的解决方案,我宁愿不要.

Any ideas on how to get this to work? I know I can create my own wrapper class(around ArrayBlockingQueue) that is not parametirized but I would rather not if there is a better way to go about solving this.

推荐答案

如果您尝试使用注释自动关联集合,请使用@Resource而不是@Autowired.

If you're trying to auto wire a collection with annotations, then use @Resource instead of @Autowired.

为了满足@Autowired集合的依赖关系,IoC容器会寻找合适的类型的元素以从中构建这样的集合.换句话说,它不查找集合本身,而是从其他bean构建一个集合.

In order to satisfy an @Autowired collection dependency, the IoC container looks for elements of the right type to build such a collection from. In other words, it does not look for the collection itself, but rather builds a collection out of other beans.

有关更多信息,请参见Spring文档,例如. 此处.

For more information, see the Spring docs, ex. here.

这篇关于春季自动接线的参数化集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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