Spring 的复杂类型的属性注入,
我们在使用Spring框架的时候除了对类进行一般的类型的属性注入,
同时还需要注入一些复杂类型的的注入
例如注入:数组,集合(List,Set,Map)
1、数组属性的注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Spring的属性注入的方式 -->
<!-- Spring数组属性的注入 -->
<!-- 注入数组类型 -->
<bean id="collectionBean" class="com.itzheng.spring.demo5.CollectionBean">
<!-- 注入数组类型 -->
<property name="arrs">
<list>
<value>王西</value>
<value>赵娜</value>
<value>李武</value>
</list>
</property>
</bean>
</beans>
设置对应的属性和set方法
package com.itzheng.spring.demo5;
import java.util.Arrays;
/*
* 集合属性的注入:
*/
public class CollectionBean {
private String[] arrs;
public void setArrs(String[] arrs) {
this.arrs = arrs;
}
@Override
public String toString() {
return "CollectionBean [arrs=" + Arrays.toString(arrs) + "]";
}
}
package com.itzheng.spring.demo5;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
* 复杂类型的属性注入
*/
public class SpringDemo5 {
@Test
public void demo1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
System.out.println(collectionBean);
}
}
2、集合属性的注入
(1) list集合当中放入普通类型的value标签,对象ref
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Spring数组属性的注入 -->
<!-- 注入数组类型 -->
<bean id="collectionBean" class="com.itzheng.spring.demo5.CollectionBean">
<!-- 注入数组类型 -->
<property name="arrs">
<list>
<value>王西</value>
<value>赵娜</value>
<value>李武</value>
</list>
</property>
<!-- 注入list集合 -->
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
<value>登峰</value>
</list>
</property>
</bean>
</beans>
设置对应的属性和set方法
package com.itzheng.spring.demo5;
import java.util.Arrays;
import java.util.List;
/*
* 集合属性的注入:
*/
public class CollectionBean {
private String[] arrs;
private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
public void setArrs(String[] arrs) {
this.arrs = arrs;
}
@Override
public String toString() {
return "CollectionBean [arrs=" + Arrays.toString(arrs) + ", list=" + list + "]";
}
}
package com.itzheng.spring.demo5;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
* 复杂类型的属性注入
*/
public class SpringDemo5 {
@Test
public void demo1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean");
System.out.println(collectionBean);
}
}
(2)Set集合和Map集合
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Spring数组属性的注入 -->
<!-- 注入数组类型 -->
<bean id="collectionBean" class="com.itzheng.spring.demo5.CollectionBean">
<!-- 注入数组类型 -->
<property name="arrs">
<list>
<value>王西</value>
<value>赵娜</value>
<value>李武</value>
</list>
</property>
<!-- 注入list集合 -->
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
<value>登峰</value>
</list>
</property>
<!-- 注入set集合 -->
<property name="set">
<set>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
<value>ddd</value>
</set>
</property>
<property name="map">
<map>
<entry key="111" value="iiii"></entry>
<entry key="222" value="zzz"></entry>
<entry key="333" value="bbb"></entry>
<entry key="444" value="ddd"></entry>
</map>
</property>
</bean>
</beans>
设置对应的属性和set方法
package com.itzheng.spring.demo5;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*
* 集合属性的注入:
*/
public class CollectionBean {
private String[] arrs;
private List<String> list;
private Set<String> set;
private Map<String, String> map;
public void setArrs(String[] arrs) {
this.arrs = arrs;
}
public void setList(List<String> list) {
this.list = list;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
@Override
public String toString() {
return "CollectionBean [arrs=" + Arrays.toString(arrs) + ", list=" + list + ", set=" + set + ", map=" + map
+ "]";
}
}