Hello everyone and thanks for the help in advance.
I am having a problem where Spring cannot autowire a parametirized member variable of type ArrayBlockingQueue.
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.
3) constructor autowiring modeIn case of constructor autowiring mode, spring container injects the dependency by highest parameterized constructor. If you have 3 constructors in a class, zero-arg, one-arg and two-arg then injection will be performed by calling the two-arg constructor.
You need to specify this bean in the constructor: @Component public class MainClass { private final AnotherClass anotherClass; // this annotation is NOT required if there is only 1 constructor, shown for clarity. @Autowired MainClass(AnotherClass anotherClass) { this.
So the answer is: No, @Autowired does not necessarily mean you must also use @Component . It may be registered with applicationContext. xml or @Configuration+@Bean .
Starting with Spring 2.5, the framework introduced annotations-driven Dependency Injection. The main annotation of this feature is @Autowired. It allows Spring to resolve and inject collaborating beans into our bean.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With