基于XMl的DI
- 匿名Bean
- 匿名内部Bean
- 同类抽象Bean
- 异类抽象Bean
- 多个配置文件-平行关系
- 多个配置文件-包含关系
匿名Bean
没有id,但是可以通过autowire="byType"找到。
即:不能通过getBean()得到这个Bean
School类
package Part03.InnerNonameBean; /** * Created by futao on 2017/10/12. */ public class School { private String sname; public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } @Override public String toString() { return "School{" + "sname='" + sname + '\'' + '}'; } }
Student类
package Part03.InnerNonameBean; /** * Created by futao on 2017/10/12. */ public class Student { private String sname; private int age; private School school; public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } @Override public String toString() { return "Student{" + "sname='" + sname + '\'' + ", age=" + age + ", school=" + school + '}'; } }
配置文件
<?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"> <!--这个bean没有id属性,所以是匿名的--> <bean class="Part03.InnerNonameBean.School"> <property name="sname" value="Ecjtu"/> </bean> <bean id="student" class="Part03.InnerNonameBean.Student" autowire="byType"> <property name="sname" value="小王"/> <property name="age" value="18"/> </bean> </beans>
测试
/** * 匿名类/匿名内部类 */ @Test fun test4NonameBean() { val classPathXmlApplicationContext = ClassPathXmlApplicationContext("applicationContextInnerNonameBean.xml") val student = classPathXmlApplicationContext.getBean("student") as Part03.InnerNonameBean.Student println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student)) }
结果
{ "sname": "小王", "age": 18, "school": { "sname": "Ecjtu" } }
匿名内部Bean
特点:
1.匿名的,所以该bean在配置文件中没有id属性,也就不能通过getBean()得到
2.内部的,该bean被其他bean的属性标签包裹,所以也无法通过autowire="byType"得到。所以这个bean只能被这个包裹他的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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="Part03.InnerNonameBean.Student"> <property name="sname" value="小王"/> <property name="age" value="18"/> <property name="school"> <!--1.这个bean没有id属性,所以是匿名的--> <!--2.这个bean是在student的school属性中,所以是内部的--> <bean class="Part03.InnerNonameBean.School"> <property name="sname" value="Ecjtu"/> </bean> </property> </bean> </beans>
同类抽象Bean
abstract="true"
Student类
package Part03.SameAbstractBean; /** * Created by futao on 2017/10/12. */ public class Student { private String sname; private int age; private String school; private String department; public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } @Override public String toString() { return "Student{" + "sname='" + sname + '\'' + ", age=" + age + ", school='" + school + '\'' + ", department='" + department + '\'' + '}'; } }
配置文件
<?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"> <!--传统方式,代码大量冗余--> <bean id="student1" class="Part03.SameAbstractBean.Student"> <property name="sname" value="张三"/> <property name="age" value="18"/> <property name="school" value="Ecjtu"/> <property name="department" value="计算机学院"/> </bean> <bean id="student2" class="Part03.SameAbstractBean.Student"> <property name="sname" value="李四"/> <property name="age" value="38"/> <property name="school" value="NCDX"/> <property name="department" value="软件学院"/> </bean> <bean id="student3" class="Part03.SameAbstractBean.Student"> <property name="sname" value="王五"/> <property name="age" value="68"/> <property name="school" value="SHJD"/> <property name="department" value="计算机科学学院"/> </bean> <!--同类抽象Bean,抽象的 abstract="true"--> <bean id="baseStudent" class="Part03.SameAbstractBean.Student" abstract="true"> <property name="school" value="Ecjtu"/> <property name="department" value="计算机科学"/> </bean> <bean id="student1-1" parent="baseStudent"> <property name="age" value="100"/> </bean> <bean id="student1-2" parent="baseStudent"> <property name="school" value="NCDX"/> </bean> </beans>
测试
/** * 同类抽象Bean */ @Test fun test4SameAbstractBean() { val classPathXmlApplicationContext = ClassPathXmlApplicationContext("applicationContextSameAbstractBean.xml") // val student1 = classPathXmlApplicationContext.getBean("student1") as Part03.SameAbstractBean.Student // println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student1)) // val student2 = classPathXmlApplicationContext.getBean("student2") as Part03.SameAbstractBean.Student // println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student2)) // val student3 = classPathXmlApplicationContext.getBean("student3") as Part03.SameAbstractBean.Student // println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student3)) val baseStudent = classPathXmlApplicationContext.getBean("baseStudent") as Part03.SameAbstractBean.Student println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(baseStudent)) val student11 = classPathXmlApplicationContext.getBean("student1-1") as Part03.SameAbstractBean.Student println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student11)) val student12 = classPathXmlApplicationContext.getBean("student1-2") as Part03.SameAbstractBean.Student println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student12)) }
结果
{ "sname": null, "age": 0, "school": "Ecjtu", "department": "计算机科学" } { "sname": null, "age": 100, "school": "Ecjtu", "department": "计算机科学" } { "sname": null, "age": 0, "school": "NCDX", "department": "计算机科学" }
异类抽象Bean
将一部分bean的某些相同的属性拿出来,组成一个bean,这个bean没有class,需要标注
abstract="true"
Student类
package Part03.DiffAbstractBean; /** * Created by futao on 2017/10/12. */ public class Student { private String name; private int age; private String school; private String department; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", school='" + school + '\'' + ", department='" + department + '\'' + '}'; } }
Teacher类
package Part03.DiffAbstractBean; /** * Created by futao on 2017/10/12. */ public class Teacher { private String name; private int wordAge; private String school; private String department; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getWordAge() { return wordAge; } public void setWordAge(int wordAge) { this.wordAge = wordAge; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } @Override public String toString() { return "Teacher{" + "name='" + name + '\'' + ", wordAge=" + wordAge + ", school='" + school + '\'' + ", department='" + department + '\'' + '}'; } }
配置文件
<?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"> <!--异类抽象Bean--> <bean id="baseBean" abstract="true"> <property name="school" value="华东交通大学111"/> <property name="department" value="计算机科学"/> </bean> <bean id="student" class="Part03.DiffAbstractBean.Student" parent="baseBean"> <property name="name" value="张三"/> <property name="age" value="23"/> <!--<property name="school" value="Ecjtu"/>--> <!--<property name="department" value="软件学院"/>--> </bean> <bean id="teacher" class="Part03.DiffAbstractBean.Teacher" parent="baseBean"> <property name="name" value="老王"/> <property name="wordAge" value="18"/> <!--<property name="department" value="软件学院"/>--> <!--<property name="school" value="Ecjtu"/>--> </bean> </beans>
测试
/** * 异类抽象Bean */ @Test fun test4DiffAbstractBean() { val classPathXmlApplicationContext = ClassPathXmlApplicationContext("applicationContextDiffAbstractBean.xml") val student = classPathXmlApplicationContext.getBean("student") as Part03.DiffAbstractBean.Student println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student)) val teacher = classPathXmlApplicationContext.getBean("teacher") as Teacher println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(teacher)) }
结果
{ "name": "张三", "age": 23, "school": "华东交通大学111", "department": "计算机科学" } { "name": "老王", "wordAge": 18, "school": "华东交通大学111", "department": "计算机科学" }
为Spring 配置多个子配置文件-平行关系
多个配置文件,加载的时候加载多个就行
配置文件1
<?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"> <bean id="student" class="Part03.ManyChildXmlFileParallel.Student"> <property name="name" value="File1"/> <property name="age" value="18"/> <property name="school" value="华东交通大学"/> <property name="department" value="软件"/> </bean> </beans>
配置文件2
<?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"> <bean id="teacher" class="Part03.ManyChildXmlFileParallel.Teacher"> <property name="name" value="File2"/> <property name="wordAge" value="28"/> <property name="department" value="计算机"/> <property name="school" value="Ecjjtu"/> </bean> </beans>
测试
/** * 为Spring配置多个配置文件-平行关系 */ @Test fun test4ManyChildXmlFile() { //使用通配符* //val classPathXmlApplicationContext = ClassPathXmlApplicationContext("applicationContextManyChildXmlFile*") val classPathXmlApplicationContext = ClassPathXmlApplicationContext("applicationContextManyChildXmlFile1.xml", "applicationContextManyChildXmlFile2.xml") val student = classPathXmlApplicationContext.getBean("student") as Part03.ManyChildXmlFileParallel.Student println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(student)) val teacher = classPathXmlApplicationContext.getBean("teacher") as Part03.ManyChildXmlFileParallel.Teacher println(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(teacher)) }
结果
{ "name": "File1", "age": 18, "school": "华东交通大学", "department": "软件" } { "name": "File2", "wordAge": 28, "school": "Ecjjtu", "department": "计算机" }
为Spring配置多个子配置文件-包含关系
在主配置中使用<import />将子配置文件导入到主配置文件中,所以程序只需要加载主配置文件。
主配置文件
<?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"> <!--导入子配置文件--> <!--<import resource="applicationContextManyChildXmlFile1.xml"/>--> <!--<import resource="applicationContextManyChildXmlFile2.xml"/>--> <!--使用通配符--> <import resource="applicationContextManyChildXmlFile*.xml"/> </beans>