基础类Teacher/Student
package com.ioc;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.Set;
public class Teacher {
//byte short int long float double char boolean
private String name;
private Integer age;
private List<Student> students;
private Set<String> jobs;
private List<Float> scores;
private String[] names;
private HashMap<String, Object> map;
private List<HashMap<String, Object>> maps;
private Properties emails;
private Date ctime;
public Teacher(){
System.out.println("teacher构造函数....");
}
public String[] getNames() {
return names;
}
public void setNames(String[] names) {
this.names = names;
}
public List<HashMap<String, Object>> getMaps() {
return maps;
}
public void setMaps(List<HashMap<String, Object>> maps) {
this.maps = maps;
}
public HashMap<String, Object> getMap() {
return map;
}
public void setMap(HashMap<String, Object> map) {
this.map = map;
}
public Set<String> getJobs() {
return jobs;
}
public void setJobs(Set<String> jobs) {
this.jobs = jobs;
}
public Properties getEmails() {
return emails;
}
public void setEmails(Properties emails) {
this.emails = emails;
}
public Date getCtime() {
return ctime;
}
public void setCtime(Date ctime) {
this.ctime = ctime;
}
public List<Float> getScores() {
return scores;
}
public void setScores(List<Float> scores) {
this.scores = scores;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
package com.ioc;
public class Student {
private String name;
private Integer classNo;
private int age;
private Float score;
private Teacher teacher;
public Student() {
super();
}
public Student(String name, int age, Float score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
public Student(String name, Integer classNo, int age, Float score, Teacher teacher) {
super();
this.name = name;
this.classNo = classNo;
this.age = age;
this.score = score;
this.teacher = teacher;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getClassNo() {
return classNo;
}
public void setClassNo(Integer classNo) {
this.classNo = classNo;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Float getScore() {
return score;
}
public void setScore(Float score) {
this.score = score;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--
bean:节点: id:唯一 class:装配需要的类,必须要有包名.类名
//ioc setter注入
<property name="属性的名称" value="属性的值"/> 普通变量的方式
<property name="属性的名称" ref="beanId"/> 对象的方式
前提:必须在类中生成setter方法即可,getter不需要.
//ioc 构造函数注入 <constructor-arg value="刘德华"></constructor-arg>
//ioc注解注入 @autowire
//ioc接口注入 接口注入,构造函数注入,setter注入目的:赋值
-->
<bean id="teacher" class="com.ioc.Teacher">
<property name="name" value="fyl1"></property>
<property name="age" value="30"></property>
<property name="students" >
<list>
<ref bean="stu1"/>
<ref bean="stu2"/>
</list>
</property>
</bean>
<bean id="stu1" class="com.ioc.Student"
p:name="stu1"
p:score="98"
p:teacher-ref="teacher">
</bean>
<bean id="stu2" class="com.ioc.Student">
<property name="name" value="stu2"></property>
<property name="score" value="88.2"></property>
<property name="teacher" ref="teacher"></property>
</bean>
<!-- 等价于 SimpleDateFormate dateFormat = new SimpleDateFormate("yyyy-MM-dd"); -->
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd HH:mm:ss" />
</bean>
<!-- 构造函数注入 -->
<bean id="stuConstructor" class="com.ioc.Student">
<constructor-arg index="0" type="java.lang.String" value="fyl-Constructor"></constructor-arg>
<constructor-arg index="1" type="int" value="12"></constructor-arg>
<constructor-arg index="2" type="java.lang.Float" value="77.2f"></constructor-arg>
</bean>
<!-- 注入Teacher类(不可 混用-只是试验) -->
<bean id="teacherM" class="com.ioc.Teacher" p:name="fylM">
<property name="age" value="20"></property>
<!-- 类引用使用ref -->
<property name="students">
<list>
<ref bean="stu1"/>
<ref bean="stu2"/>
</list>
</property>
<!-- set 不允许重复(语文老师只会出现一个) -->
<property name="jobs">
<set>
<value>语文老师</value>
<value>语文老师</value>
<value>英语老师</value>
</set>
</property>
<property name="scores">
<list>
<value>34</value>
<value>45.78</value>
<value>34.234</value>
</list>
</property>
<!-- array数组(简写:p:names="小A,小B") -->
<property name="names">
<array>
<value>小A</value>
<value>小B</value>
</array>
</property>
<!-- map注入 -->
<property name="map">
<map>
<entry>
<key><value>username</value></key>
<value>fylMap</value>
</entry>
<entry>
<key><value>age</value></key>
<value>22</value>
</entry>
</map>
</property>
<!-- ListMap注入 -->
<property name="maps">
<list>
<map>
<entry>
<key><value>username</value></key>
<value>fylListMap</value>
</entry>
<entry>
<key><value>age</value></key>
<value>32</value>
</entry>
</map>
<map>
<entry>
<key><value>username</value></key>
<value>mm</value>
</entry>
<entry>
<key><value>age</value></key>
<value>17</value>
</entry>
</map>
</list>
</property>
<!-- Properties注入 -->
<property name="emails">
<props>
<prop key="Aemail">123.qq.com</prop>
<prop key="Bemail">456.qq.com</prop>
</props>
</property>
<!-- Date注入 -->
<property name="ctime">
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="2016-12-14 11:00:12" />
</bean>
</property>
</bean>
</beans>
测试类
package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ioc.Student;
import com.ioc.Teacher;
public class Test {
public static void main(String[] args) {
//Teacher与Student 建立联系
// Teacher teacher = new Teacher();
// teacher.setName("fyl");
// teacher.setAge(11);
// teacher.setStudents(null);
//
// Student student1 = new Student();
// student1.setName("学生一");
// student1.setTeacher(teacher); //绑定师生关系
// Student student2 = new Student();
// student2.setName("学生一");
// student2.setTeacher(teacher); //绑定师生关系
//
// List<Student> listStu = new ArrayList<>();
// listStu.add(student1);
// listStu.add(student2);
// teacher.setStudents(listStu);
//
// for (Student student : teacher.getStudents()) {
// System.out.println(teacher.getName()+" 有学生: "+student.getName());
// }
//IOC bean注入 加载时即初始化类
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml");
// Teacher teacher = (Teacher) context.getBean("teacher");
// for (Student student : teacher.getStudents()) {
// System.out.println(teacher.getName()+" 的年龄是:"+teacher.getAge()+" 有学生: "+student.getName()+" 分数:"+student.getScore());
// }
//
// Student student = (Student) context.getBean("stu1");
// System.out.println(student.getName()+" 的老师是: "+student.getTeacher().getName());
// Student stuConstructor = (Student) context.getBean("stuConstructor");
// System.out.println(stuConstructor.getName()+"====>"+stuConstructor.getScore());
Teacher teacherM = (Teacher) context.getBean("teacherM");
System.out.println(teacherM.getName()+"==>"+teacherM.getAge());
for (int i = 0; i < teacherM.getStudents().size(); i++) {
System.out.println("学生:"+teacherM.getStudents().get(i).getName()+"==>"+teacherM.getStudents().get(i).getScore());
}
for (String job:teacherM.getJobs()){
System.out.println(job);
}
for (Float score:teacherM.getScores()){
System.out.println(score);
}
for (int i = 0; i < teacherM.getNames().length; i++) {
System.out.println(teacherM.getNames()[i]);
}
for (Map.Entry<String, Object> map : teacherM.getMap().entrySet()) {
System.out.println(map);
System.out.println(map.getKey()+"<==>"+map.getValue()+"<==>"+teacherM.getMap().get(map.getKey()));
}
List<HashMap<String, Object>> listMap = teacherM.getMaps();
for (HashMap<String, Object> hashMap : listMap) {
for (Map.Entry<String, Object> hmap:hashMap.entrySet()) {
System.out.println(hmap.getKey()+"<==>"+hmap.getValue());
}
}
Properties properties = teacherM.getEmails();
System.out.println(properties.getProperty("Aemail"));
System.out.println(properties.getProperty("Bemail"));
System.out.println(teacherM.getCtime());
}
}
运行 结果:
十二月 14, 2016 10:59:10 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@579bb367: startup date [Wed Dec 14 10:59:10 CST 2016]; root of context hierarchy
十二月 14, 2016 10:59:10 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
teacher构造函数.... //加载bean.xml时,初始化的类
teacher构造函数....
fylM==>20
学生:stu1==>98.0
学生:stu2==>88.2
语文老师
英语老师
34.0
45.78
34.234
小A
小B
username=fylMap
username<==>fylMap<==>fylMap
age=22
age<==>22<==>22
username<==>fylListMap
age<==>32
username<==>mm
age<==>17
123.qq.com
456.qq.com
Wed Dec 14 11:00:12 CST 2016