Spring学习日记03_IOC_属性注入_集合类型属性
本文内容纲要:Spring学习日记03_IOC_属性注入_集合类型属性
Ioc操作Bean管理(xml注入集合属性)
- 注入数组类型属性
- 注入List集合类型属性
- 注入Map集合类型属性
Stu类
public class Stu { //1. 数组类型属性
private String[] courses;
//2. List集合类型属性
private List<String> list;
//3. Map集合类型属性
private Map<String,String> maps;
//3. Set集合类型属性
private Set<String> sets;
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;
}
}
xml
<!--集合类型属性注入--> <bean id="stu" class="Spring.Ioc.Day04.collection.Stu">
<!--数组-->
<property name="courses" >
<array>
<value>java</value>
<value>c++</value>
</array>
</property>
<!--list-->
<property name="list">
<list>
<value>1</value>
<value>2</value>
</list>
</property>
<!--Map-->
<property name="maps">
<map>
<entry key="1" value="one"></entry>
<entry key="2" value="two"></entry>
</map>
</property>
<!--Set-->
<property name="sets">
<set>
<value>mysql</value>
<value>oracle</value>
</set>
</property>
</bean>
</beans>
- 在集合里面设置对象类型值
xml
<bean> <!--注入list集合类型,值是对象-->
<property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<!--创建多个course对象-->
<bean id="course1" class="Spring.Ioc.Day04.collection.Course">
<property name="cname" value="Spring"></property>
</bean>
<bean id="course2" class="Spring.Ioc.Day04.collection.Course">
<property name="cname" value="MVC"></property>
</bean>
- 把集合注入部分提取出来
- 在spring配置文件中引入名称空间util
- 使用util标签完成list集合注入提取
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"
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>book1</value>
<value>book2</value>
<value>book3</value>
</util:list>
<!--2 提取list集合类型属性注入使用-->
<bean id="book" class="Spring.Ioc.Day05.collection.book">
<property name="list" ref="bookList"></property>
</bean>
</beans>
book类
public class book { private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
public void test(){
System.out.println(list);
}
}
本文内容总结:Spring学习日记03_IOC_属性注入_集合类型属性
原文链接:https://www.cnblogs.com/iron1213/p/14970396.html
以上是 Spring学习日记03_IOC_属性注入_集合类型属性 的全部内容, 来源链接: utcz.com/z/296035.html