Spring 自动装配一个列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6267138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:32:13  来源:igfitidea点击:

Spring autowire a list

springlistautowired

提问by karq

Is it possible to use @Autowiredwith a list?

是否可以@Autowired与列表一起使用?

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

就像我有带有 mimetypes 的属性文件,在我的类文件中我有这样的东西

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

回答by Tho

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

Spring 4 支持自动收集给定类型的所有 bean 并将它们注入集合或数组的能力。

Example:

例子:

@Component
public class Car implements Vehicle {
}

@Component
public class Bus implements Vehicle {
}

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

Ref: Spring 4 Ordering Autowired Collections

参考:Spring 4 订购 Autowired 集合

回答by Johan Sj?berg

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

@Qualifier("..")不鼓励,而是尝试使用按名称自动装配

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

See also How to autowire factorybean.

另请参阅如何自动装配 factorybean

回答by chzbrgla

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

您甚至可以java.util.List在 spring .xml 中创建一个并将其注入@Qualifier到您的应用程序中。从 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>[email protected]</value>
   <value>[email protected]</value>
   <value>[email protected]</value>
   <value>[email protected]</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?

如果要注入属性,请查看如何将属性值注入使用注释配置的 Spring Bean?

回答by duffymo

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.

我认为您至少需要一个限定符。对“new”的调用似乎与使用 Spring 的想法相反。您对 Spring 的角色感到困惑。如果您调用“new”,则该对象不受 Spring 的控制。

回答by stuchl4n3k

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

如果自动装配的 bean 在同一个 ( @Configuration) 类中声明,并且您需要它来声明另一个 bean,那么以下工作:

@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 mimeTypesbean in another config. No need for explicit @Qualifieror @Resourceannotations.

自然地,即使您mimeTypes在另一个配置中覆盖了bean,它的行为也是正确的。不需要显式@Qualifier@Resource注释。

回答by sblundy

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

只要列表是一个 bean,您就应该能够自动装配它。然后,您将使用@Qualifier告诉 Spring 要使用哪个 bean/list。请参阅http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#beans-autowired-annotation-qualifiers