Spring DI(依赖注入)详解

依赖注入(DI)


  • 依赖注入(Dependency Injection,DI)。
  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

一、构造器注入(默认)

之前使用无参构造方法或有参构造方法的方式 就是构造器注入,具体可以参照之前的博客

二、Set方式注入【重点】

要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型, 没有set方法 , 是 is .

  • 普通值注入
     <bean id="student" class="com.lding.pojo.Student">
            <property name="name" value="冷丁">property>
     bean>       
    
  • Bean 注入
    注意 这里的值是一个引用 ref
    <bean id="addr" class="com.lding.pojo.Address">
        <property name="address" value="北京">property>
    bean>
     <bean id="student" class="com.lding.pojo.Student">
        <property name="address" ref="addr">property>
    bean>    
    
  • 数组注入
     <bean id="student" class="com.lding.pojo.Student">
    	<property name="books">
                <array>
                    <value>操作系统value>
                    <value>计算机网路value>
                    <value>深入理解计算机系统value>
                array>
            property>
    bean>    
    
  • List注入
     <bean id="student" class="com.lding.pojo.Student">
    	<property name="hobbies">
            <list>
                <value>编程value>
                <value>看电影value>
                <value>旅行value>
            list>
        property>
    bean>    
    
  • map 注入
     <bean id="student" class="com.lding.pojo.Student">
    	 <property name="card">
                <map>
                    <entry key="身份证" value="2102732312312312">entry>
                    <entry key="学生卡" value="179321803">entry>
                map>
            property>
    bean>    
    
  • set注入
    <bean id="student" class="com.lding.pojo.Student">
    	<property name="games">
           <set>
                <value>LOLvalue>
                <value>CFvalue>
                <value>DNFvalue>
            set>
          property>
    bean>    
    
  • null 注入
     <bean id="student" class="com.lding.pojo.Student">
         <property name="wife">
            <null>null>
        property>
    bean>    
    
  • Properties注入
     <bean id="student" class="com.lding.pojo.Student">
    	<property name="info">
           <props>
                <prop key="学号">21321312prop>
                <prop key="姓名">冷丁prop>
                <prop key="性别">prop>
            props>
          property>
    bean>    
    

接下来完成一个Demo 实现set注入
测试类 Address.java

package com.lding.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

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

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

测试类 Student.java

package com.lding.pojo;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

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

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +"\n"+
                ", books=" + Arrays.toString(books)+"\n"+
                ", hobbies=" + hobbies +"\n"+
                ", card=" + card+"\n"+
                ", games=" + games+"\n"+
                ", wife='" + wife + '\'' +
                ", info=" + info +"\n"+
                '}';
    }
}

test.java

public class Mytest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }
}

运行结果

Spring DI(依赖注入)详解_第1张图片

如果对您有帮助,免费的赞点一个~~~感谢

Spring DI(依赖注入)详解_第2张图片

你可能感兴趣的:(Spring精华详解,Java后端学习精华,spring,java,后端)