Spring 配置bean传数组、map、List、set
一、普通注入
Student类定义:(加入set方法)
注入自定义类
<!-- set注入自定义类 -->
<property name="set">
<set>
<ref bean="bean的id"/>
</set>
</property>
<!-- list注入自定义类 -->
<property name="bookDaoImpl">
<list>
<ref bean="book1"/>
<ref bean="book2"/>
</list>
</property>
<!-- map注入自定义类 -->
<property name="map">
<map>
<entry key="map4" key-ref="bean的id"/>
</map>
</property>
<!-- properties注入属性值写入标签内 -->
<property name="properties">
<props>
<prop key="test">ppp</prop>
</props>
</property>
public class Student {
private String[] courses;
private List<String> list;
private Map<String, String> maps;
private Set<String> sets;
private List<Course> listCourse;
public void setListCourse(List<Course> listCourse) {
this.listCourse = listCourse;
}
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="Student" class="com.zhuRu.Student">
<!--数组类型注入-->
<property name="courses">
<array>
<value>JAVA</value>
<value>Mysql</value>
<value>高数</value>
<value>线代</value>
<value>概率论</value>
</array>
</property>
<!--数组类型注入-->
<property name="list">
<list>
<value>list注入1</value>
<value>list注入2</value>
<value>list注入3</value>
</list>
</property>
<!--set类型注入-->
<property name="sets">
<set>
<value>set注入1</value>
<value>set注入2</value>
<value>set注入3</value>
<value>set注入4</value>
</set>
</property>
<!--map类型注入-->
<property name="maps">
<map>
<entry key="map注入1" value="Java"></entry>
<entry key="map注入2" value="Mysql"></entry>
<entry key="map注入3" value="数学"></entry>
<entry key="map注入4" value="英语"></entry>
</map>
</property>
</bean>
二、util注入(可以重复使用List、Map等)
Book类:
public class Book {
private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
public void test() {
System.out.println(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">
<util:list id="bookList">
<value>高数</value>
<value>数学</value>
<value>英语</value>
</util:list>
<bean id="Book" class="com.zhuRu.Book">
<property name="list" ref="bookList"></property>
</bean>
<!--定义工厂bean-->
<bean id="MyBean" class="com.factoryBean.MyBean">
</bean>
</beans>
三、引入外部xml文件
<import resource="applicationContext-user.xml"/>
四、spring的xml配置总结
<beans>
<bean>
id属性不可重复
class属性 类的全路径
scope属性 可配置request / session / global session
<property>
name属性 属性名
value属性 属性值
ref属性 注入对象的引用值
<list>
<value>list值</value>
<ref>
bean属性 注入对象的引用值
</ref>
</list>
<set>
<value></value>
<ref>
bean属性 注入对象的引用值
</ref>
</set>
<map>
<entry>
key属性
value属性
key-ref属性
</entry>
</map>
<props> 注入Properties类型数据
<prop key="test">ppp</prop>
</props>
</property>
</bean>
<beans>
<import resource="applicationContext-user.xml"/>