Spring Autowiring of Parameterized Collection
Hello everyone and thanks for the help in advance.
I am having a problem where Spring cannot autowire a parametirized member variable of type ArrayBlockingQue开发者_开发知识库ue.
Here is the java code:
@Controller
public class SomeController
{
@Autowired
private ArrayBlockingQueue<SomeCustomType> myQueue;
}
and in the spring configuration xml:
<bean id="myQueue" class="java.util.concurrent.ArrayBlockingQueue">
<constructor-arg value="10"/>
</bean>
Specifying the type (SomeCustomType) for the ArrayBlockingQueue seems to confuse spring which fails to find a match and does not perform the autowiring.
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.
If you're trying to auto wire a collection with annotations, then use @Resource
instead of @Autowired
.
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.
For more information, see the Spring docs, ex. here.
精彩评论