具有集合性质的属性注入
String数组
<!-- String -->
<property name="mystr">
<array>
<value>北京</value>
<value>上海</value>
</array>
</property>
Set
<!-- Set<String> -->
<property name="myset">
<set>
<value>朝阳区</value>
<value>大兴区</value>
</set>
</property>
List
<!-- List<Student> -->
<property name="mylist">
<list>
<ref bean="myStudent1"/>
<ref bean="myStudent2"/>
</list>
</property>
Map
<!-- Map<String,Integer> -->
<property name="mymap">
<map>
<entry key="height" value="180"/>
<entry key="weight" value="60"/>
</map>
</property>
Properties
<!-- Properties -->
<property name="myprop">
<props>
<prop key="tel">77788899676</prop>
<prop key="phone">13566677676</prop>
</props>
</property>
不难看出,其实就是你是什么集合,就用什么标签,注意一下各自注入值的方式就好。
示例:
MyCollection.java
package com.ba04;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class MyCollections {
private String [] mystr;
private Set<String> myset;
private List<Student> mylist;
private Map<String,Integer> mymap;
private Properties myprop; //key和value都是String的map
public void setMystr(String[] mystr) {
this.mystr = mystr;
}
public void setMyset(Set<String> myset) {
this.myset = myset;
}
public void setMylist(List<Student> mylist) {
this.mylist = mylist;
}
public void setMymap(Map<String, Integer> mymap) {
this.mymap = mymap;
}
public void setMyprop(Properties myprop) {
this.myprop = myprop;
}
@Override
public String toString() {
return "MyCollections [mystr=" + Arrays.toString(mystr) + ", myset=" + myset + ", mylist=" + mylist + ", mymap="
+ mymap + ", myprop=" + myprop + "]";
}
}
Student.java
package com.ba04;
public class Student {
private String name;
private int age;
//引用类型
private School mySchool;
public Student() {
super();
System.out.println("student无参数构造方法");
}
//定义有参数的构造方法
public Student(String myname ,int myage, School myXueXiao){
System.out.println("有参数的Student构造方法");
//属性赋值
this.name = myname;
this.age = myage;
this.mySchool = myXueXiao;
}
public void setName(String name) {
System.out.println("setName:"+name);
this.name = name;
}
public void setAge(int age) {
System.out.println("setAge:"+age);
this.age = age;
}
public void setMySchool(School mySchool) {
System.out.println("setMySchool:"+mySchool);
this.mySchool = mySchool;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", mySchool=" + mySchool + "]";
}
}
School.java
package com.ba04;
public class School {
private String name;
private String address;
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "School [name=" + name + ", address=" + address + "]";
}
}
aplicationContext.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
声明MyCollections
-->
<bean id="myCollections" class="com.ba04.MyCollections">
<!-- String -->
<property name="mystr">
<array>
<value>北京</value>
<value>上海</value>
</array>
</property>
<!-- Set<String> -->
<property name="myset">
<set>
<value>朝阳区</value>
<value>大兴区</value>
</set>
</property>
<!-- List<Student> -->
<property name="mylist">
<list>
<ref bean="myStudent1"/>
<ref bean="myStudent2"/>
</list>
</property>
<!-- Map<String,Integer> -->
<property name="mymap">
<map>
<entry key="height" value="180"/>
<entry key="weight" value="60"/>
</map>
</property>
<!-- Properties -->
<property name="myprop">
<props>
<prop key="tel">77788899676</prop>
<prop key="phone">13566677676</prop>
</props>
</property>
<!-- List<Map<String, String>> mylistmap -->
<property name="mylistmap">
<list>
<map> <!-- index=0 -->
<entry key="height" value="180cm"></entry>
<entry key="weight" value="70kg"></entry>
</map>
<map> <!-- index=1 -->
<entry key="tel" value="0662-67678888"></entry>
<entry key="phone" value="13498987897"></entry>
</map>
</list>
</property>
</bean>
<!-- 使用name,完成构造注入 -->
<bean id="myStudent1" class="com.ba04.Student">
<!-- 实现构造注入:
name:构造方法的形参名称
value:简单类型参数的值
ref:引用类型参数的值
-->
<constructor-arg name="myname" value="李四" />
<constructor-arg name="myXueXiao" ref="myXueXiao1" />
<constructor-arg name="myage" value="22" />
</bean>
<!-- 使用index,完成构造注入 -->
<bean id="myStudent2" class="com.ba04.Student">
<!-- 实现构造注入
index:构造方法的参数位置, 从0开始。
value:简单类型参数的值
ref:引用类型参数的值
-->
<constructor-arg index="1" value="20" />
<constructor-arg index="0" value="张三" />
<constructor-arg index="2" ref="myXueXiao1" />
</bean>
<bean id="myXueXiao1" class="com.ba04.School">
<property name="name" value="北京大学" />
<property name="address" value="北京的海淀区" />
</bean>
</beans>
MyTest.java
package com.ba04;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test01(){
String configLocation="com/ba04/applicationContext.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
MyCollections myCollections = (MyCollections) ctx.getBean("myCollections");
System.out.println("myColl:" + myCollections);
}
}
List里有两个学生
复杂型集合的属性注入
List<Map<String, String>>
对于这种就是嵌套,第一层是什么就用什么标签,再看第二层,然后使用对应的注入方式进行值的注入
<!-- List<Map<String, String>> mylistmap -->
<property name="mylistmap">
<list>
<map> <!-- index=0 -->
<entry key="height" value="180cm"></entry>
<entry key="weight" value="70kg"></entry>
</map>
<map> <!-- index=1 -->
<entry key="tel" value="0662-67678888"></entry>
<entry key="phone" value="13498987897"></entry>
</map>
</list>
</property>
结束语
每个人心里都有一团火,路过的人只看到烟