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>LOL</value>
                <value>CF</value>
                <value>DNF</value>
            </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="学号">21321312</prop>
                <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());
    }
}

运行结果

在这里插入图片描述

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

在这里插入图片描述

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值