参数化集合的Spring自动装配

Qua*_*ent 6 java spring

各位大家好,感谢您的帮助.

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

这是java代码:

@Controller
public class SomeController
{
    @Autowired
    private ArrayBlockingQueue<SomeCustomType> myQueue;
}
Run Code Online (Sandbox Code Playgroud)

并在spring配置xml中:

<bean id="myQueue" class="java.util.concurrent.ArrayBlockingQueue">
    <constructor-arg value="10"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

为ArrayBlockingQueue指定类型(SomeCustomType)似乎会混淆spring无法找到匹配并且不执行自动装配.

有关如何使其工作的任何想法?我知道我可以创建我自己的包装类(围绕ArrayBlockingQueue),这个类没有参数化但我宁愿不知道是否有更好的方法来解决这个问题.

jto*_*ron 12

如果您尝试使用注释自动连接集合,则使用@Resource而不是@Autowired.

为了满足@Autowired集合依赖性,IoC容器会查找正确类型的元素以构建此类集合.换句话说,它不会查找集合本身,而是从其他bean构建集合.

有关更多信息,请参阅Spring文档,例如.在这里.