Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autowiring a collection via the constructor with Spring

Tags:

I have what seems to be a simple problem, as stated in the title. Here is the kind of class I have :

public class Foo {     @Autowired     public Foo(@Qualifier("bar") Set<String> bar) {         // ...     } } 

Which I try to run with the following spring context :

<context:annotation-config /> <util:set id="bar">     <value>tata</value>     <value>titi</value>     <value>toto</value> </util:set> <bean id="foo" class="Foo" /> 

This fails to run with :

No matching bean of type [java.lang.String] found for dependency [collection of java.lang.String]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=bar)}

Note that if I add other parameters to my constructor, it works fine. If I use setter injection, it works fine. I'm sure I miss something obvious ... do you know what ?

like image 669
Guillaume Avatar asked Jul 29 '10 15:07

Guillaume


People also ask

Can we do @autowired by constructor?

In Spring, you can use @Autowired annotation to auto-wire bean on the setter method, constructor , or a field . Moreover, it can autowire the property in a particular bean. We must first enable the annotation using below configuration in the configuration file.

How do you use an Autowired constructor?

This mode is very similar to byType, but it applies to constructor arguments. Spring container looks at the beans on which autowire attribute is set constructor in the XML configuration file. It then tries to match and wire its constructor's argument with exactly one of the beans name in the configuration file.

How do you call an Autowired constructor?

P.S: You can also have a constructor with parameters if you use the @Autowired annotation. On this case, Spring will call this constructor to create the bean and pass the required parameters if there are such beans declared that can be autowired into the constructor.

What is @autowired annotation in Spring?

Enabling @Autowired annotation By declaring beans, you provide metadata to the Spring Container to return the required dependency object at runtime. This is called Spring Bean Autowiring. In java based configuration, all the bean methods are defined in the class with @configuration annotation.


2 Answers

Autowiring collections is not possible using the @Autowired annotation. An autowired collection means "to provide all beans of a particular type". Using the JSR-250 @Resource annotation, you can declare that you want a resource injected by its name, not its type. Or you inject the dependency explicitly.

[...] beans which are themselves defined as a collection or map type cannot be injected via @Autowired since type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection/map bean by unique name.

See the Spring documentation for more details.

like image 94
Christian Semrau Avatar answered Oct 02 '22 11:10

Christian Semrau


As others stated it is impossible to use @Autowired for Strings and collections of String. You can use @Value with spring EL here assuming you have spring in version 3:

 public class Foo {     @Autowired     public Foo(@Value("#{bar}") Set<String> bar) {         // ...     } } 
like image 34
mrembisz Avatar answered Oct 02 '22 12:10

mrembisz