39

Is it possible to use @Autowired with a list?

Like I have properties file with mimetypes and in my class file I have something like this

@Autowired
private List<String> mimeTypes = new ArrayList<String>();
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
karq
  • 849
  • 2
  • 14
  • 27
  • 10
    It's been a while - if any of the answers was helpful, please mark it as correct, so that others with the same question can easily identify any helpful answers. – chzbrgla Jul 21 '11 at 08:01
  • 1
    possible duplicate of [Auto-wiring a List using util schema gives NoSuchBeanDefinitionException](http://stackoverflow.com/questions/1363310/auto-wiring-a-list-using-util-schema-gives-nosuchbeandefinitionexception) – skaffman Feb 04 '12 at 12:15

6 Answers6

66

Spring 4 and older support the ability to automatically collect all beans of a given type and inject them into a collection or array.

Here is an example:

@Component
public class Car implements Vehicle {
}

@Component
public class Bus implements Vehicle {
}

@Component
public class User {
   @Autowired
   List<Vehicle> vehicles; // contains both car and bus
}

Ref: Spring 4 Ordering Autowired Collections

Tho
  • 23,158
  • 6
  • 60
  • 47
34

@Qualifier("..") is discouraged, instead try to autowire-by-name using

private @Resource(name="..") List<Strings> mimeTypes;

See also How to autowire factorybean.

Community
  • 1
  • 1
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
13

You can even create a java.util.List within your spring .xml and inject this via @Qualifier into your application. From the springsource http://static.springsource.org/spring/docs/current/reference/xsd-config.html :

 <!-- creates a java.util.List instance with the supplied values -->
 <util:list id="emails">
   <value>pechorin@hero.org</value>
   <value>raskolnikov@slums.org</value>
   <value>stavrogin@gov.org</value>
   <value>porfiry@gov.org</value>
 </util:list>

So this would change your wiring to:

 @Autowired
 @Qualifier("emails")
 private List<String> mimeTypes = new ArrayList<String>();

I would suggest this approach since you're injecting a list of strings anyways.

cheers!

EDIT

If you want to inject properties, have a look at this How can I inject a property value into a Spring Bean which was configured using annotations?

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
chzbrgla
  • 5,158
  • 7
  • 39
  • 56
1

I think you'll need a qualifier at minimum. And the call to "new" seems contrary to the idea of using Spring. You have Spring's role confused. If you call "new", then the object isn't under Spring's control.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • It just overwrites the whole object. Spring didn't know (or didn't care) that there was something there before. – Donal Fellows Jun 07 '11 at 15:15
  • Using prototype-scoped beans, you can even use the `new` and still have that bean spring managed btw http://static.springsource.org/spring/docs/current/reference/beans.html#beans-factory-scopes-prototype – chzbrgla Jun 07 '11 at 15:19
0

If the autowired bean is declared in the same (@Configuration) class and you need it to declare another bean, then following works:

@Bean
public BeanWithMimeTypes beanWithMimeTypes() {
    return new BeanWithMimeTypes(mimeTypes());
}

@Bean
public List<String> mimeTypes() {
    return Arrays.<String>asList("text/html", "application/json);
}

Naturally it behaves correctly even if you override the mimeTypes bean in another config. No need for explicit @Qualifier or @Resource annotations.

stuchl4n3k
  • 588
  • 5
  • 14
  • 1
    This is simply showing how to use a constructor (which is ok). But it needs to be pointed out that in this example there is not need to annotate mimeTypes() with @Bean unless you want its result available in the application context for some other reason than creating the BeanWithMimeTypes component. – Hans-Peter Schmid Jun 17 '17 at 04:52
-1

You should be able to autowire it as long as the list is a bean. You'd then use the @Qualifier to tell Spring which bean/list to use. See http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#beans-autowired-annotation-qualifiers

sblundy
  • 60,628
  • 22
  • 121
  • 123