xml注入集合属性
1.注入数组类型属性
2.注入list集合类型属性
3.注入Map集合类型属性
(1)创建类,定义数组、list、map、set类型属性,生成对应set方法
package com.atguigu.spring5;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Stu {
//1.数组类型属性
private String[] courses;
//2.list集合类型属性
private List<String> list;
//3.map集合类型属性
private Map<String,String> maps;
//4.set集合类型属性
private Set<String> sets;
//set方法
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
}
(2)在spring配置文件进行配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="stu" class="com.atguigu.spring5.Stu">
<!--数组类型属性注入-->
<property name="courses">
<array>
<value>java课程</value>
<value>数据库课程</value>
</array>
</property>
<!--list集合类型属性注入-->
<property name="list">
<list>
<value>张三</value>
<value>小三</value>
</list>
</property>
<!--map集合类型属性注入-->
<property name="maps">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="PHP" value="php"></entry>
</map>
</property>
<!-- set集合类型属性-->
<property name="sets">
<set>
<value>MySQL</value>
<value>Redis</value>
</set>
</property>
</bean>
</beans>
(3)创建测试类进行测试
package com.atguigu.spring5.testdemo;
import com.atguigu.spring5.collecticntype.Stu;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;
import javax.naming.Context;
public class TestSpring5Demo1 {
@Test
public void testCollection(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Stu stu = context.getBean("stu",Stu.class);
stu.test();
}
}
4.在集合里面设置对象类型值
<bean id="stu" class="com.atguigu.spring5.collecticntype.Stu">
<!-- 注入list集合类型,值是对象-->
<property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<!-- 创建多个course对象-->
<bean id="course1" class="com.atguigu.spring5.collecticntype.Course">
<property name="cname" value="Spring5框架"></property>
</bean>
<bean id="course2" class="com.atguigu.spring5.collecticntype.Course">
<property name="cname" value="MyBatis框架"></property>
</bean>
5.把集合注入部分提取出来
(1)在spring配置文件中引入名称空间util
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--集合类型属性注入-->
</beans>
(2)使用util标签完成list集合注入提取
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--1.提取list集合类型属性注入-->
<util:list id="bookList">
<value>易筋经</value>
<value>九阴真经</value>
<value>九阳神功</value>
<value>神雕侠侣</value>
</util:list>
<!--2.提取list集合类型属性注入使用-->
<bean id="book" class="com.atguigu.spring5.collecticntype.Book">
<property name="list" ref="bookList"></property>
</bean>
</beans>
(3)创建测试类
package com.atguigu.spring5.testdemo;
import com.atguigu.spring5.collecticntype.Book;
import com.atguigu.spring5.collecticntype.Stu;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.Test;
import javax.naming.Context;
public class TestSpring5Demo1 {
@Test
public void testCollection2(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
Book book = context.getBean("book",Book.class);
book.test();
}
}