Spring DI(依赖注入) 笔记

DI依赖注入

​ 依赖注入可以理解成IoC的一种应用场景,反转的是对象间依赖关系维护权。讲的通俗点,就是在运行期,由Spring根据配置文件,将其他对象的引用通过组件的提供的setter方法进行设定。

1. set方法注入

在要注入属性的bean标签中进行配置。前提是该类有提供属性对应的set方法。

实体类代码

public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private Teacher teacher;

    public Student(){}

    public Student(Integer id, String name, Integer age, Teacher teacher) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.teacher = teacher;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

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

public class Teacher {
    private Integer id;
    private String name;
    private Integer age;

    public Teacher() {
    }

    public Teacher(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

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

配置文件内容

<?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">

  <!--set方法注入teacher类的值-->
    <bean class="com.zijie.domain.Teacher" id="teacher">
        <property name="id" value="2" />
        <property name="name" value="李四" />
    </bean>

    <!--set方法注入student类的值-->
        <!--
            name属性用来指定要设置哪个属性
            value属性用来设置要设置的值
            ref属性用来给引用类型的属性设置值,可以写上Spring容器中bean的id
        -->
    <bean class="com.zijie.domain.Student" id="student">
        <property name="id" value="1" />
        <property name="name" value="张三" />
        <property name="age" value="23"/>
        <property name="teacher" ref="teacher"/>
    </bean>
      
</beans>

测试类

public class APP {
    public static void main(String[] args) {
        //1.获取StudentDaoImpl对象
        //创建Spring容器,指定要读取的配置文件路径
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器中获取对象
        Student student = (Student) app.getBean("student");
        //调用对象的方法进行测试
        System.out.println(student);
    }
}

2.有参构造注入

在要注入属性的bean标签中进行配置。前提是该类有提供对应的有参构造。
使用上面的Student类进行有参构造的设置

配置文件内容

<?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 class="com.zijie.domain.Teacher" id="teacher">
        <property name="id" value="2" />
        <property name="name" value="李四" />
    </bean>
    <!--使用有参构造进行注入 constructor-arg 构造器注入-->
    <bean class="com.zijie.domain.Student" id="student">
        <constructor-arg name="id" value="1" />
        <constructor-arg name="name" value="张三" />
        <constructor-arg name="age" value="23" />
        <constructor-arg name="teacher" ref="teacher" />
    </bean>
</beans>

测试类

public class APP {
    public static void main(String[] args) {

        //1.获取StudentDaoImpl对象
        //创建Spring容器,指定要读取的配置文件路径
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器中获取对象
        Student student = (Student) app.getBean("student");
        //调用对象的方法进行测试
        System.out.println(student);
    }
}

3.复杂类型属性注入

定义两个实体类:Test和Massages

public class Massages {
    private String name;
    private String password;
    private String path;
    private double phone;

    public Massages() {
    }

    public Massages(String name, String password, String path, double phone) {
        this.name = name;
        this.password = password;
        this.path = path;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public double getPhone() {
        return phone;
    }

    public void setPhone(double phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Massages{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", path='" + path + '\'' +
                ", phone=" + phone +
                '}';
    }
}

import java.util.*;
public class Test {
    private int age;
    private String name;
    private Massages massages;
    private List<String> list;
    private List<Massages> phones;
    private Set<String> set;
    private Map<String, Massages> map;
    private int[] arr;
    private Properties properties;

    public Test() {
    }

    public Test(int age, String name, Massages massages, List<String> list, List<Massages> phones, Set<String> set, Map<String, Massages> map, int[] arr, Properties properties) {
        this.age = age;
        this.name = name;
        this.massages = massages;
        this.list = list;
        this.phones = phones;
        this.set = set;
        this.map = map;
        this.arr = arr;
        this.properties = properties;
    }

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    public Massages getMassages() {
        return massages;
    }

    public void setMassages(Massages massages) {
        this.massages = massages;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public List<Massages> getPhones() {
        return phones;
    }

    public void setPhones(List<Massages> phones) {
        this.phones = phones;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public Map<String, Massages> getMap() {
        return map;
    }

    public void setMap(Map<String, Massages> map) {
        this.map = map;
    }

    public int[] getArr() {
        return arr;
    }

    public void setArr(int[] arr) {
        this.arr = arr;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "Test{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", massages=" + massages +
                ", list=" + list +
                ", phones=" + phones +
                ", set=" + set +
                ", map=" + map +
                ", arr=" + Arrays.toString(arr) +
                ", properties=" + properties +
                '}';
    }
}

在配置文件中设置相应的属性值

<?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 class="com.zijie.domain.Massages" id="massages">
        <property name="name" value="王五"/>
        <property name="password" value="123456"/>
        <property name="path" value="beijing"/>
        <property name="phone" value="12234345678"/>
    </bean>
    <bean class="com.zijie.domain.Test" id="test">
        <!-- 普通类型 -->
        <property name="age" value="22"/>
        <property name="name" value="张三"/>

        <!-- Massages massages -->
        <property name="massages" ref="massages"/>

        <!-- list<String> list -->
        <property name="list">
            <list>
                <value>刘备</value>
                <value>关羽</value>
                <value>张飞</value>
            </list>
        </property>

        <!-- List<Massages> phones-->
        <property name="phones">
            <list>
                <ref bean="massages"></ref>
            </list>
        </property>

        <!-- Set<String> set-->
        <property name="set">
            <set>
                <value>111</value>
                <value>222</value>
            </set>
        </property>

        <!-- Map<String, Massages> map-->
        <property name="map">
            <map>
                <entry key="1" value-ref="massages"></entry>
            </map>
        </property>

        <!-- int[] arr-->
        <property name="arr">
            <array>
                <value>1</value>
                <value>2</value>
            </array>
        </property>

        <!-- Properties properties-->
        <property name="properties">
            <props>
                <prop key="one">第一</prop>
                <prop key="two">第二</prop>
            </props>
        </property>
    </bean>
</beans>

配置测试类

public class APP {
    public static void main(String[] args) {
        //1.获取StudentDaoImpl对象
        //创建Spring容器,指定要读取的配置文件路径
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器中获取对象

        Test test = (Test) app.getBean("test");
        //调用对象的方法进行测试
        System.out.println(test);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值