1. Student实体类
package org.spring;
public class Student {
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [name=" + name + "]";
}
}
2. Teacher实体类
package org.spring;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Teacher {
private String name;
private List list;
private Set<Student> set;
private Map<String,Student> map;
private Student[] array;
private Properties properties;
public void setName(String name) {
this.name = name;
}
public void setList(List list) {
this.list = list;
}
public void setSet(Set<Student> set) {
this.set = set;
}
public void setMap(Map<String, Student> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public String getName() {
return name;
}
public List getList() {
return list;
}
public Set<Student> getSet() {
return set;
}
public Map<String, Student> getMap() {
return map;
}
public Properties getProperties() {
return properties;
}
public void setArray(Student[] array) {
this.array = array;
}
public Student[] getArray() {
return array;
}
}
3. 配置Bean
<?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"
xmlns:p="http://www.springframework.org/schema/p"
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-4.0.xsd">
<beans>
<!--Utils命名空间声明可以被引用的List,通过id引用-->
<util:list id="StudentList">
<bean class="org.spring.Student">
<property name="name" value="list1"></property>
</bean>
<ref bean="student"/>
</util:list>
<bean id="student" class="org.spring.Student">
<property name="name" value="小明同学"></property>
</bean>
<bean id="teacher" class="org.spring.Teacher">
<property name="name" value="老师"></property>
<!-- 配置List -->
<property name="list">
<list>
<ref bean="student"/>
<bean class="org.spring.Student">
<property name="name"><value>小红同学</value></property>
</bean>
</list>
</property>
<!-- 配置Set -->
<property name="set">
<set>
<ref bean="student"/>
<bean class="org.spring.Student">
<property name="name" value="小黑同学"></property>
</bean>
</set>
</property>
<!-- 配置Map -->
<property name="map">
<map>
<entry key="key01" value-ref="student"></entry>
<entry key="key02">
<bean class="org.spring.Student">
<property name="name" value="小晓同学"></property>
</bean>
</entry>
</map>
</property>
<!-- 配置 properties -->
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="passwd">1234</prop>
<prop key="url"> jdbc:mysql://test//</prop>
<prop key="driver">com.jdbc.mysql.driver</prop>
</props>
</property>
<!-- 配置引用数组 -->
<property name="array">
<array>
<ref bean="student"/>
<bean class="org.spring.Student">
<property name="name" value="张三同学"></property>
</bean>
</array>
</property>
</bean>
</beans>
</beans>
4、获取TeacherBean中配置的Map List Set Properties属性
package org.spring;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student)applicationContext.getBean("student");
System.out.println(student);
Teacher teacher = (Teacher)applicationContext.getBean("teacher");
List<Student> list = teacher.getList();
System.out.println(list);
Set<Student> set = teacher.getSet();
System.out.println(set);
Map<String,Student> map = teacher.getMap();
System.out.println(map);
Properties properties = teacher.getProperties();
System.out.println(properties);
Student[] array = teacher.getArray();
System.out.println(array);
}
}