Spring 依赖注入笔记

Spring 依赖注入(1)

spring的依赖注入分类两类

1.基于xml的注入
2.基于注解的注入

本篇文章主要是讲解第一篇基于xml的注入

一.注入方式分类

注入分为设值注入和和构造注入
设值注入 指调用setter的方法传入被调用者的实例
构造注入指的是构造调用者实例的时候,完成被调用者地 实例化
说白了,第一个是用setter方法传的值,第二个是使用了有参构造方法

二.设值注入的实现

本文中,使用学生类Student 和学校类School 作为示范
Student

public class Student {
    private  String name ;
    private  int age;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

在resource中的spring 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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- -->
    <bean id="student" class="com.yuyi.test.Student"></bean>
</beans>

在test类中进行测试

    public void  test01(){

        String config = "test/spring.xml";
        //创建spring对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        Student ser = (Student) ctx.getBean("student");
        System.out.println("Student"+ser);

    }

测试结果如图,我们发现,此时的实体类没有给属性赋值,均为默认赋值。
在这里插入图片描述
此时,我们开始给属性赋值,语法为下表所示

    <bean id="student" class="com.yuyi.test.Student">
        <property name="name" value="TOM"/>
        <property name="age" value="18"/>

    </bean>

这时,我们运行test,所得结果如下
在这里插入图片描述
如果在此时的setter方法中将this.x=x注释掉,我们会发现,程序依然能运行,但是不会给属性赋值,甚至没有的属性,只要有这个setter方法,依然会运行所以说,这种方法是根据属性的名字去寻找setter方法的,而setter是程序员自己决定的。

上例的属性赋值是简单赋值(基本数据类型和String类),下面我们学习属性是引用类型的赋值。
School类

public class School {
    
    private  String address;
    private  String name;

    public void setAddress(String address) {
        this.address = address;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "School{" +
                "address='" + address + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

修改后的Student

public class Student {
    private  String name ;
    private  int age;
    private School school;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", school=" + school +
                '}';
    }
}

此时我们需要对spring的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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- -->
    <bean id="student" class="com.yuyi.test.Student" >
        <property name="name" value="TOM"/>
        <property name="age" value="18"/>
        <property name="school" ref="school"/>
    </bean>
    <bean id="school" class="com.yuyi.test.School">
        <property name="address" value="北京"/>
        <property name="name" value="清华大学"/>
    </bean>
</beans>

由上可以看出,引用数据类型是将本来value的地方改为ref =“bean的id”
同时将声明的School类进行赋值
运行test
在这里插入图片描述

三.构造注入的实现

此时将Student类写一个有参的构造方法

    public Student(String name, int age, School school) {
        this.name = name;
        this.age = age;
        this.school = school;
    }

此时,构造注入不会调用无参构造方法,而是调用有参构造来实现。
此时我们在xml文件中不会使用property属性进行设置,而是使用constructor-arg 进行设置,
配置的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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- -->
    <bean id="student" class="com.yuyi.test.Student" >
        <constructor-arg name="age" value="18"/>
        <constructor-arg name="name" value="tom"/>
        <constructor-arg name="school" ref="school"/>

    </bean>
    <bean id="school" class="com.yuyi.test.School">
        <constructor-arg name="address" value="北京" />
        <constructor-arg name="name" value="清华大学" />

    </bean>
</beans>

运行test 的结果如下
在这里插入图片描述
配置xml还有另外一种方法
使用index属性

    <bean id="student" class="com.yuyi.test.Student" >
        <constructor-arg index="1" value="18"/>
        <constructor-arg index="0" value="tom"/>
        <constructor-arg index="2" ref="school"/>

此时使用的index是有参构造的参数位置,结果如
在这里插入图片描述
一摸一样。。

猜你喜欢

转载自blog.csdn.net/qq_39428182/article/details/105736632