欢迎移步博客查看-http://futaosmile.oschina.io/coder957
基于XMl的DI
- 匿名Bean
- 匿名内部Bean
- 同类抽象Bean
- 异类抽象Bean
- 多个配置文件-平行关系
- 多个配置文件-包含关系
Java中的匿名内部类
匿名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 +
'}';
}
}
配置文件
测试
/**
* 匿名类/匿名内部类
*/
@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所使用
修改配置文件
同类抽象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 + '\'' +
'}';
}
}
配置文件
测试
/**
* 同类抽象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 + '\'' +
'}';
}
}
配置文件
测试
/**
* 异类抽象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
配置文件2
测试
/**
* 为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配置多个子配置文件-包含关系
在主配置中使用
将子配置文件导入到主配置文件中,所以程序只需要加载主配置文件。
主配置文件