Spring Autowiring of Parameterized Collection

We welcome everyone and thank you in advance for your help.

I am having a problem where Spring cannot autodetect a parameterized member variable of type ArrayBlockingQueue.

Here is the java code:

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

and in Spring xml configuration:

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

Setting the type (SomeCustomType) for an ArrayBlockingQueue seems to confuse Spring, which does not find a match and does not auto-increment.

Any ideas on how to make this work? I know that I can create my own wrapper class (around ArrayBlockingQueue) that is not parameterized, but I would prefer if there is a better way to solve this.

+6
source share
1 answer

If you are trying to automatically create a collection with annotations, use @Resource instead of @Autowired .

To satisfy the dependencies of the @Autowired collection, the IoC container looks for elements of the correct type to build such a collection. In other words, he is not looking for the collection itself, but building a collection of other beans.

For more information, see Spring docs, for example. here .

+12
source

Source: https://habr.com/ru/post/898634/


All Articles