• 【Spring框架】——4.依赖注入(DI)



    依赖注入(Dependency Injection,简称 DI) :依赖指的是 bean 对象的创建依赖于容器;注入指的是 bean 对象中的所有属性,由容器进行注入。

    依赖注入的实现方式,常见的有以下几种:

    • 基于构造器(也叫构造函数、构造方法)的注入
    • 基于 setter 的注入
    • 基于命名空间的注入
    • 基于注解的注入

    (基于注解的注入,这里暂时不做展开演示,它的本质就是使用注解实现自动装配,即用注解实现 bean 属性的自动注入)

    1. 基于构造器注入

    新建一个类——Student.java

    package com.yuhuofei.pojo;
    
    /**
     * @Description
     * @ClassName Student
     * @Author yuhuofei
     * @Date 2022/6/21 22:33
     * @Version 1.0
     */
    public class Student {
    
        /**
         * 学生姓名
         */
        private String studentName;
        /**
         * 学生学号
         */
        private Integer studentNo;
    
        /**
         * 有参构造方法
         */
        public Student(String name, Integer number) {
            this.studentName = name;
            this.studentNo = number;
        }
    
        /**
         * 控制台打印出学生的信息
         */
        public void printStudent() {
            System.out.println("学生的名字:" + studentName);
            System.out.println("学生的学号:" + studentNo);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    新建一个 xml 文件—— beans.xml

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--普通实体类的bean注册-->
        <bean id="student" class="com.yuhuofei.pojo.Student">
            <constructor-arg index="0" value="yuhuofei"/>
            <constructor-arg index="1" value="2022061801"/>
        </bean>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    新建一个测试类——TestStudent.java

    import com.yuhuofei.pojo.Student;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @Description
     * @ClassName TestSpring
     * @Author yuhuofei
     * @Date 2022/6/20 21:08
     * @Version 1.0
     */
    public class TestStudent {
    
        public static void main(String[] args) {
    
            //拿到Spring的容器
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    
            //从容器中获取student对象
            Student student = (Student) context.getBean("student");
            student.printStudent();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    运行测试类,结果如下

    在这里插入图片描述
    由以上的示例,可以看出,在 beans.xml 文件中,直接通过对构造器的操作,实现了值的注入。

    2. 基于 setter 的注入

    在前面的基础上,我们改变一下各个文件的内容,演示基于 set 方法的注入

    Student.java

    package com.yuhuofei.pojo;
    
    import java.util.Arrays;
    
    /**
     * @Description
     * @ClassName Student
     * @Author yuhuofei
     * @Date 2022/6/21 22:33
     * @Version 1.0
     */
    public class Student {
    
        /**
         * 学生姓名
         */
        private String studentName;
        /**
         * 学生学号
         */
        private Integer studentNo;
    
        /**
         * 课程列表
         */
        private String[] course;
    
        public String getStudentName() {
            return studentName;
        }
    
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }
    
        public Integer getStudentNo() {
            return studentNo;
        }
    
        public void setStudentNo(Integer studentNo) {
            this.studentNo = studentNo;
        }
    
        public String[] getCourse() {
            return course;
        }
    
        public void setCourse(String[] course) {
            this.course = course;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "studentName='" + studentName + '\'' +
                    ", studentNo=" + studentNo +
                    ", course=" + Arrays.toString(course) +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    beans.xml

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--普通实体类的bean注册-->
        <bean id="student" class="com.yuhuofei.pojo.Student">
            <!--基于set方法注入值-->
            <property name="studentName" value="yuhuofei"/>
            <property name="studentNo" value="2022061801"/>
            <property name="course">
                <array>
                    <value>大学物理</value>
                    <value>高等数学</value>
                    <value>概率论</value>
                    <value>通信原理</value>
                    <value>数据库概论</value>
                    <value>工程制图</value>
                    <value>计算机导论</value>
                    <value>操作系统</value>
                    <value>信息论</value>
                    <value>计算机网络</value>
                    <value>C语言程序设计</value>
                    <value>JAVA程序设计</value>
                </array>
            </property>
        </bean>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    TestStudent.java

    import com.yuhuofei.pojo.Student;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @Description
     * @ClassName TestSpring
     * @Author yuhuofei
     * @Date 2022/6/20 21:08
     * @Version 1.0
     */
    public class TestStudent {
    
        public static void main(String[] args) {
    
            //拿到Spring的容器
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    
            //从容器中获取student对象
            Student student = (Student) context.getBean("student");
            System.out.println("学生的名字是:" + student.getStudentName());
            System.out.println("学生的信息是:" + student);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    运行测试类的结果

    在这里插入图片描述
    由以上示例可以看出,基于 setter 的注入,其实就是利用类里面的 set 方法为类中的成员变量进行赋值,而赋值的操作是在 beans.xml 文件中指定的。

    3. 基于命名空间的注入

    3.1 p 命名空间的注入

    新建 Course.java

    package com.yuhuofei.pojo;
    
    /**
     * @Description
     * @ClassName Course
     * @Author yuhuofei
     * @Date 2022/6/22 21:03
     * @Version 1.0
     */
    public class Course {
    
        private String courseName;
    
        private Integer courseNo;
    
        public String getCourseName() {
            return courseName;
        }
    
        public void setCourseName(String courseName) {
            this.courseName = courseName;
        }
    
        public Integer getCourseNo() {
            return courseNo;
        }
    
        public void setCourseNo(Integer courseNo) {
            this.courseNo = courseNo;
        }
    
        @Override
        public String toString() {
            return "Course{" +
                    "courseName='" + courseName + '\'' +
                    ", courseNo=" + courseNo +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    新建 courseBeans.xml

    <?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:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--    p命名空间注入-->
        <bean id="course" class="com.yuhuofei.pojo.Course" p:courseName="语文" p:courseNo="0001"/>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    注意,一定要在 xml 文件中,加入下面这一行,才能使用 p 命名空间注入

    xmlns:p="http://www.springframework.org/schema/p"
    
    • 1

    新建 TestCourse.java

    import com.yuhuofei.pojo.Course;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @Description
     * @ClassName TestCourse
     * @Author yuhuofei
     * @Date 2022/6/22 21:05
     * @Version 1.0
     */
    public class TestCourse {
    
        public static void main(String[] args) {
    
    		//拿到spring的容器
            ApplicationContext context = new ClassPathXmlApplicationContext("courseBeans.xml");
    		//从容器中获取对象
            Course course = context.getBean("course", Course.class);
    		//输出对象
            System.out.println(course);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    运行测试类,就能打印输出 course 对象了。

    3.2 c 命名空间的注入

    改动 Course.java 类的内容

    package com.yuhuofei.pojo;
    
    /**
     * @Description
     * @ClassName Course
     * @Author yuhuofei
     * @Date 2022/6/22 21:03
     * @Version 1.0
     */
    public class Course {
    
        private String courseName;
    
        private Integer courseNo;
    
        public Course(String name,Integer num){
            this.courseName=name;
            this.courseNo=num;
        }
    
        @Override
        public String toString() {
            return "Course{" +
                    "courseName='" + courseName + '\'' +
                    ", courseNo=" + courseNo +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    改动 courseBeans.xml 的内容

    <?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:c="http://www.springframework.org/schema/c"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!--    c命名空间注入-->
    <bean id="course" class="com.yuhuofei.pojo.Course" c:name="数学" c:num="0002"/>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    注意,要加入下面这一行,才能使用 c 命名空间

    xmlns:c="http://www.springframework.org/schema/c"
    
    • 1

    测试类则保持 3.1 中的不变,直接执行,即可得到新的结果。

  • 相关阅读:
    【Linux】进程信号
    【云计算网络安全】僵尸网络详解:工作原理、控制和保护方法
    Nanoprobes丨Nanogold 标记条带的凝胶染色
    sequencer和sequence
    Groovy语法&Gradle配置学习笔记
    分享好用的Cloudflare测速文件
    统计页面刷新次数
    喜报 | 博睿数据两项发明专利获得国家知识产权局授权,累计发明专利11项
    VisionMaster自定义模块
    javaScript排他tab页
  • 原文地址:https://blog.csdn.net/Crezfikbd/article/details/125382513