前面的注入解决大部分的问题。但是注入比如数组、集合等的操作是操作不了的。现在就看看这些复杂类型的注入。
比如 map list 数组 properties等
其实就是在响应的属性下 使用相应的标签
例如:
<bean name="com2" class="com.qwm.spring1.bean.ComplexBean">
<property name="arr">
<array>
<value>wimingvalue>
<value>qiwenmingvalue>
<value>xiaomingvalue>
array>
property>
<property name="list">
<list>
<value>小明value>
<value>小小明value>
<value>小明明value>
list>
property>
<property name="map">
<map>
<entry key="userName" value="wiming"/>
<entry key="mycar" value-ref="car"/>
<entry key-ref="car" value-ref="car"/>
map>
property>
<property name="prop">
<props>
<prop key="p1">p11111prop>
<prop key="p2">p22222prop>
<prop key="p3">p33333prop>
props>
property>
bean>
下面测试,使用到 ComplexBean和Car类。
package com.qwm.spring1.bean;
/**
* @author: wiming
* @date: 2017-09-27 14:59:50 星期三
* @decription:
*/
public class Car {
private String color;
private String name;
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Override public String toString() { return "Car{color='" + color + '\'' + ", name='" + name + '\'' + '}'; }
}
package com.qwm.spring1.bean;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* @author: wiming
* @date: 2017-09-27 16:10:40 星期三
* @decription:
* 用于测试复杂类型注入
*/
public class ComplexBean implements Serializable {
private Object[] arr;
private List list;//List/Set 一样的
private Map map;
private Properties prop;
//get/set方法
public Object[] getArr() { return arr; }
public void setArr(Object[] arr) { this.arr = arr; }
public List getList() { return list; }
public void setList(List list) { this.list = list; }
public Map getMap() { return map; }
public void setMap(Map map) { this.map = map; }
public Properties getProp() { return prop; }
public void setProp(Properties prop) { this.prop = prop; }
@Override
public String toString() { return "ComplexBean{arr=" + Arrays.toString(arr) + ", list=" + list + ", map=" + map + ", prop=" + prop + '}'; }
}
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="car" class="com.qwm.spring1.bean.Car">
<property name="name" value="兰博基尼"/>
<property name="color" value="红色"/>
bean>
<bean name="com1" class="com.qwm.spring1.bean.ComplexBean">
<property name="arr" value="arr_one"/>
<property name="list" value="list_one"/>
<property name="map">
<map>
<entry key="userName" value="wiming"/>
<entry key="mycar" value-ref="car"/>
<entry key-ref="car" value-ref="car"/>
map>
property>
<property name="prop">
<props>
<prop key="p1">p11111prop>
<prop key="p2">p22222prop>
<prop key="p3">p33333prop>
props>
property>
bean>
beans>
/**
* 复杂类型注入
* 其中 array 和list只有一个的情况
*/
@Test
public void test8(){
ApplicationContext ac = new ClassPathXmlApplicationContext("com/qwm/spring1/c_inject/applicationContext.xml");
ComplexBean com1 = (ComplexBean) ac.getBean("com1");
System.out.println(com1);
}
ComplexBean{arr=[arr_one], list=[list_one], map={userName=wiming, mycar=Car{color='红色', name='兰博基尼'}, Car{color='红色', name='兰博基尼'}=Car{color='红色', name='兰博基尼'}}, prop={p3=p33333, p2=p22222, p1=p11111}}
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<bean name="car" class="com.qwm.spring1.bean.Car">
<property name="name" value="兰博基尼"/>
<property name="color" value="红色"/>
bean>
<bean name="com2" class="com.qwm.spring1.bean.ComplexBean">
<property name="arr">
<array>
<value>wimingvalue>
<value>qiwenmingvalue>
<value>xiaomingvalue>
array>
property>
<property name="list">
<list>
<value>小明value>
<value>小小明value>
<value>小明明value>
list>
property>
<property name="map">
<map>
<entry key="userName" value="wiming"/>
<entry key="mycar" value-ref="car"/>
<entry key-ref="car" value-ref="car"/>
map>
property>
<property name="prop">
<props>
<prop key="p1">p11111prop>
<prop key="p2">p22222prop>
<prop key="p3">p33333prop>
props>
property>
bean>
beans>
/**
* 复杂类型注入
* 其中 array 和list有多个的情况
*/
@Test
public void test9(){
ApplicationContext ac = new ClassPathXmlApplicationContext("com/qwm/spring1/c_inject/applicationContext.xml");
ComplexBean com2 = (ComplexBean) ac.getBean("com2");
System.out.println(com2);
}
ComplexBean{arr=[wiming, qiwenming, xiaoming], list=[小明, 小小明, 小明明], map={userName=wiming, mycar=Car{color='红色', name='兰博基尼'}, Car{color='红色', name='兰博基尼'}=Car{color='红色', name='兰博基尼'}}, prop={p3=p33333, p2=p22222, p1=p11111}}
https://github.com/wimingxxx/spring01/tree/master/src/com/qwm/spring1/c_inject