Spring 注入集合
1. 介绍¶
你已经看到了如何使用 value 属性来配置基本数据类型和在你的 bean 配置文件中使用
现在如果你想传递多个值,如 Java Collection 类型 List、Set、Map 和 Properties,应该怎么做呢。为了处理这种情况,Spring 提供了四种类型的集合的配置元素,如下所示:
元素 | 描述 |
---|---|
<list> | 它有助于连线,如注入一列值,允许重复。 |
<set> | 它有助于连线一组值,但不能重复。 |
<map> | 它可以用来注入名称-值对的集合,其中名称和值可以是任何类型。 |
<props> | 它可以用来注入名称-值对的集合,其中名称和值都是字符串类型。 |
你可以使用或
你会遇到两种情况(a)传递集合中直接的值(b)传递一个 bean 的引用作为集合的元素。
<!--给复杂类型进行赋值--> <bean id="person9" class="com.cmz.bean.Nperson"> <property name="id" value="9"></property> <property name="name" value="李9"></property> <property name="age" value="29"></property> <property name="gender" value="男"></property> <property name="date" value="2020/02/28"></property> <!--给数组赋值--> <!--<property name="hobbies" value="book,girl,movie"></property>--> <property name="hobbies"> <array> <value>book</value> <value>girl</value> <value>movie</value> </array> </property> <!--给引用类型赋值 可以使用ref引用外部bean--> <property name="address" ref="address"></property> <!--给list赋值--> <!--写法1--> <!--<property name="lists" value="1,2,3"></property>--> <!--写法2--> <property name="lists"> <list> <!--使用内部bean 无非从IOC容器中直接获取对象的值--> <bean class="com.cmz.bean.Address"> <property name="province" value="江苏省"></property> <property name="city" value="南京市"></property> <property name="town" value="下关"></property> </bean> <bean class="com.cmz.bean.Address"> <property name="province" value="河北省"></property> <property name="city" value="北京市"></property> <property name="town" value="朝阳大街"></property> </bean> <!--使用外部bean 可以随意从IOC容器获取对象的值--> <ref bean="address"></ref> </list> </property> <!--给set赋值--> <property name="sets"> <set> <value>张三</value> <value>张三</value> <value>李四</value> <value>王五</value> <value>赵六</value> <value>陈七</value> </set> </property> <!--给map赋值--> <property name="maps"> <map> <entry key="a" value="aaa"></entry> <entry key="address" value-ref="address"></entry> <entry key="address2"> <bean id="address2" class="com.cmz.bean.Address"> <property name="province" value="广东省"></property> </bean> </entry> <entry> <key> <value>my_key</value> </key> <value>my_value</value> </entry> <entry key="list"> <list> <value>11</value> <value>22</value> </list> </entry> </map> </property> <!--给properties赋值--> <property name="properties"> <props> <prop key="111">aaa</prop> <prop key="222">bbb</prop> <prop key="333">ccc</prop> </props> </property> </bean>
2. 示例¶
ioc1.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.cmz.bean.Person"> <property name="id" value="1"></property> <property name="name" value="张三"></property> <property name="age" value="20"></property> <property name="gender" value="男"></property> <property name="date" value="2020/02/28"></property> </bean> <!--给复杂类型进行赋值--> <bean id="person9" class="com.cmz.bean.Nperson"> <property name="id" value="9"></property> <property name="name" value="李9"></property> <property name="age" value="29"></property> <property name="gender" value="男"></property> <property name="date" value="2020/02/28"></property> <!--给数组赋值--> <!--<property name="hobbies" value="book,girl,movie"></property>--> <property name="hobbies"> <array> <value>book</value> <value>girl</value> <value>movie</value> </array> </property> <!--给引用类型赋值 可以使用ref引用外部bean--> <property name="address" ref="address"></property> <!--给list赋值--> <!--写法1--> <!--<property name="lists" value="1,2,3"></property>--> <!--写法2--> <property name="lists"> <list> <!--使用内部bean 无非从IOC容器中直接获取对象的值--> <bean class="com.cmz.bean.Address"> <property name="province" value="江苏省"></property> <property name="city" value="南京市"></property> <property name="town" value="下关"></property> </bean> <bean class="com.cmz.bean.Address"> <property name="province" value="河北省"></property> <property name="city" value="北京市"></property> <property name="town" value="朝阳大街"></property> </bean> <!--使用外部bean 可以随意从IOC容器获取对象的值--> <ref bean="address"></ref> </list> </property> <!--给set赋值--> <property name="sets"> <set> <value>张三</value> <value>张三</value> <value>李四</value> <value>王五</value> <value>赵六</value> <value>陈七</value> </set> </property> <!--给map赋值--> <property name="maps"> <map> <entry key="a" value="aaa"></entry> <entry key="address" value-ref="address"></entry> <entry key="address2"> <bean id="address2" class="com.cmz.bean.Address"> <property name="province" value="广东省"></property> </bean> </entry> <entry> <key> <value>my_key</value> </key> <value>my_value</value> </entry> <entry key="list"> <list> <value>11</value> <value>22</value> </list> </entry> </map> </property> <!--给properties赋值--> <property name="properties"> <props> <prop key="111">aaa</prop> <prop key="222">bbb</prop> <prop key="333">ccc</prop> </props> </property> </bean> <bean id="address" class="com.cmz.bean.Address"> <property name="province" value="江苏省"></property> <property name="city" value="宿迁市"></property> <property name="town" value="来龙镇"></property> </bean> <!--bean之间的继承关系--> <!--可以使用abstract标签定义抽象bean,无法进行实例化【abstract="true"】--> <bean id="parent" class="com.cmz.bean.Nperson" abstract="false"> <property name="id" value="1"></property> <property name="name" value="张三"></property> <property name="age" value="20"></property> <property name="gender" value="男"></property> <property name="date" value="2020/02/28"></property> </bean> <!--可以通过parent属性来获取父bean中的某些属性值--> <bean id="son" class="com.cmz.bean.Nperson" parent="parent"> <property name="name" value="儿子"></property> </bean> <!--创建bean的时候依赖关系 当bean对象被创建的时候,是按照xml配置文件定义的顺序创建的 谁在前就先被创建,如果需要干扰创建顺序,就使用depend-on属性 一般不用在意bean创建的顺序,无论谁先创建,需要依赖的对象在创建完成之后都会进行赋值 --> <bean id="adress10" class="com.cmz.bean.Address"></bean> <bean id="person10" class="com.cmz.bean.Nperson"></bean> <bean id="adress11" class="com.cmz.bean.Address" depends-on="person11"></bean> <bean id="person11" class="com.cmz.bean.Nperson"></bean> </beans>
Nperson.class
package com.cmz.bean; import java.util.*; /** * @author summer * @create 2020-02-22 20:58 */ public class Nperson{ private int id; private String name; private Integer age; private String gender; private Date date; private String[] hobbies; private Address address; private List<Address> lists; private Set<String> sets; private Map<String,Object> maps; private Properties properties; public Nperson() { System.out.println("Nperson 被创建"); } public Nperson(int id, String name, Integer age, String gender, Date date, String[] hobbies, Address address, List<Address> lists, Set<String> sets, Map<String, Object> maps, Properties properties) { this.id = id; this.name = name; this.age = age; this.gender = gender; this.date = date; this.hobbies = hobbies; this.address = address; this.lists = lists; this.sets = sets; this.maps = maps; this.properties = properties; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String[] getHobbies() { return hobbies; } public void setHobbies(String[] hobbies) { this.hobbies = hobbies; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public List<Address> getLists() { return lists; } public void setLists(List<Address> lists) { this.lists = lists; } public Set<String> getSets() { return sets; } public void setSets(Set<String> sets) { this.sets = sets; } public Map<String, Object> getMaps() { return maps; } public void setMaps(Map<String, Object> maps) { this.maps = maps; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + ", date=" + date + ", hobbies=" + Arrays.toString(hobbies) + ", address=" + address + ", lists=" + lists + ", sets=" + sets + ", maps=" + maps + ", properties=" + properties + '}'; } }
Address.class
package com.cmz.bean; /** * @author summer * @create 2020-02-28 18:24 */ public class Address { private String province; private String city; private String town; public Address() { System.out.println("Address 被创建"); } public Address(String province, String city, String town) { this.province = province; this.city = city; this.town = town; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getTown() { return town; } public void setTown(String town) { this.town = town; } @Override public String toString() { return "Address{" + "province='" + province + '\'' + ", city='" + city + '\'' + ", town='" + town + '\'' + '}'; } }
MyTest1.class
import com.cmz.bean.Nperson; import com.cmz.bean.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author summer * @create 2020-02-22 21:06 */ public class MyTest1 { public static void main(String[] args) { ApplicationContext context9 = new ClassPathXmlApplicationContext("ioc1.xml"); Nperson person9 = context9.getBean("person9", Nperson.class); System.out.println(person9); } }
输出[稍微格式化了一下]
Person { id = 9, name = '李9', age = 29, gender = '男', date = Fri Feb 28 00: 00: 00 CST 2020, hobbies = [book, girl, movie], address = Address { province = '江苏省', city = '宿迁市', town = '来龙镇' }, lists = [Address { province = '江苏省', city = '南京市', town = '下关' }, Address { province = '河北省', city = '北京市', town = '朝阳大街' }, Address { province = '江苏省', city = '宿迁市', town = '来龙镇' }], sets = [张三, 李四, 王五, 赵六, 陈七], maps = { a = aaa, address = Address { province = '江苏省', city = '宿迁市', town = '来龙镇' }, address2 = Address { province = '广东省', city = 'null', town = 'null' }, my_key = my_value, list = [11, 22] }, properties = { 333 = ccc, 222 = bbb, 111 = aaa } }