Spring自动连接列表 [英] Spring autowire a list

查看:79
本文介绍了Spring自动连接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to use @Autowired with a list?

就像我有带有mimetypes的属性文件一样,在我的班级文件中,我也有类似的东西

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>();

推荐答案

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

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

示例:

@Component
public class Car implements Vehicle {
}

@Component
public class Bus implements Vehicle {
}

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

参考: Spring 4订购自动布线的收藏

这篇关于Spring自动连接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆