Spring_DI_XML_03

简介: 欢迎移步博客查看-http://futaosmile.oschina.io/coder957基于XMl的DI匿名Bean匿名内部Bean同类抽象Bean异类抽象Bean多个配置文件-平行关系多个配置文件-包含关系Java中的匿名内部类匿名Bean没有id,但是可以通过autowire="byType"找到。

欢迎移步博客查看-http://futaosmile.oschina.io/coder957

基于XMl的DI

  1. 匿名Bean
  2. 匿名内部Bean
  3. 同类抽象Bean
  4. 异类抽象Bean
  5. 多个配置文件-平行关系
  6. 多个配置文件-包含关系

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 +
                '}';
    }
}

配置文件

<?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>
目录
相关文章
|
6月前
|
Java Spring
Spring5深入浅出篇:Spring中ioc(控制反转)与DI(依赖注入)
Spring5深入浅出篇:Spring中ioc(控制反转)与DI(依赖注入)
|
6月前
|
Java API Spring
Spring6-IoC(Inversion of Control)控制反转和DI(Dependency Injection)依赖注入,手动实现IOC
Spring6-IoC(Inversion of Control)控制反转和DI(Dependency Injection)依赖注入,手动实现IOC
|
30天前
|
存储 Java 程序员
SpringIOC和DI的代码实现,Spring如何存取对象?@Controller、@Service、@Repository、@Component、@Configuration、@Bean DI详解
本文详细讲解了Spring框架中IOC容器如何存储和取出Bean对象,包括五大类注解(@Controller、@Service、@Repository、@Component、@Configuration)和方法注解@Bean的用法,以及DI(依赖注入)的三种注入方式:属性注入、构造方法注入和Setter注入,并分析了它们的优缺点。
20 0
SpringIOC和DI的代码实现,Spring如何存取对象?@Controller、@Service、@Repository、@Component、@Configuration、@Bean DI详解
|
30天前
|
存储 开发框架 Java
什么是Spring?什么是IOC?什么是DI?IOC和DI的关系? —— 零基础可无压力学习,带源码
文章详细介绍了Spring、IOC、DI的概念和关系,解释了控制反转(IOC)和依赖注入(DI)的原理,并提供了IOC的代码示例,阐述了Spring框架作为IOC容器的应用。
22 0
什么是Spring?什么是IOC?什么是DI?IOC和DI的关系? —— 零基础可无压力学习,带源码
|
6月前
|
XML Java 数据格式
Spring框架入门:IoC与DI
【5月更文挑战第15天】本文介绍了Spring框架的核心特性——IoC(控制反转)和DI(依赖注入)。IoC通过将对象的创建和依赖关系管理交给容器,实现解耦。DI作为IoC的实现方式,允许外部注入依赖对象。文章讨论了过度依赖容器、配置复杂度等常见问题,并提出通过合理划分配置、使用注解简化管理等解决策略。同时,提醒开发者注意过度依赖注入和循环依赖,建议适度使用构造器注入和避免循环引用。通过代码示例展示了注解实现DI和配置类的使用。掌握IoC和DI能提升应用的灵活性和可维护性,实践中的反思和优化至关重要。
329 4
|
2月前
|
Java 数据库连接 数据格式
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
IOC/DI配置管理DruidDataSource和properties、核心容器的创建、获取bean的方式、spring注解开发、注解开发管理第三方bean、Spring整合Mybatis和Junit
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
|
2月前
|
Java 数据库连接 Maven
Spring基础1——Spring(配置开发版),IOC和DI
spring介绍、入门案例、控制反转IOC、IOC容器、Bean、依赖注入DI
Spring基础1——Spring(配置开发版),IOC和DI
|
5月前
|
存储 Java 测试技术
Java Spring IoC&DI :探索Java Spring中控制反转和依赖注入的威力,增强灵活性和可维护性
Java Spring IoC&DI :探索Java Spring中控制反转和依赖注入的威力,增强灵活性和可维护性
38 1
|
5月前
|
存储 Java 程序员
Spring IoC&DI(2)—IoC详解
Spring IoC&DI(2)—IoC详解
40 2
|
5月前
|
存储 Java C++
理解SpringIOC和DI第一课(Spring的特点),IOC对应五大注解,ApplicationContext vs BeanFactory
理解SpringIOC和DI第一课(Spring的特点),IOC对应五大注解,ApplicationContext vs BeanFactory