依赖注入DI

依赖注入是什么?
依赖注入,全名Dependency Injection,简称DI。所谓依赖注入,就是由IOC容器在运行期间,动态地将某种依赖关系注入到对象之中。可以这样理解,IOC(控制反转)是一种思想,依赖注入是实现IOC这个思想的一个工具,他们之间是紧密相连的。
下面用一个简单的例子来理解并使用依赖注入。

创建实体类Student和Address类

package com.muzili.pojo;

import java.util.*;

public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    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> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    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 Properties getInfo() {
        return info;
    }

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

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

package com.muzili.pojo;

public class Address {

    private String address;
    //默认存在无参构造
    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }
}

在spring核心配置文件中进行依赖注入

<?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"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--Address-->
    <bean id="addr" class="com.muzili.pojo.Address">
        <property name="address" value="深圳"/>
    </bean>
    <!--Student-->
    <bean id="student" class="com.muzili.pojo.Student">
        <!--普通字段-->
        <property name="name" value="刘德华"/>
        <!--引用其他bean使用ref-->
        <property name="address" ref="addr"/>

        <!--数组的注入-->
        <property name="books">
            <array>
                <value>西游记</value>
                <value>水浒传</value>
                <value>红楼梦</value>
                <value>三国演义</value>
            </array>
        </property>

        <!--List注入-->
        <property name="hobbys">
            <list>
                <value>运动</value>
                <value>代码</value>
                <value>电影</value>
                <value>音乐</value>
            </list>
        </property>

        <!--Map的注入-->
        <property name="card">
            <map>
                <entry key="IdCard" value="666666888888884444"/>
                <entry key="银行卡" value="111122223333444"/>
            </map>
        </property>

        <!--Set注入-->
        <property name="games">
            <set>
                <value>王者荣耀</value>
                <value>贪玩蓝月</value>
                <value>绝地求生</value>
                <value>LOL</value>
            </set>
        </property>

        <!--Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">201932301</prop>
                <prop key="性别">男</prop>
                <prop key="姓名">小明</prop>
            </props>
        </property>

    </bean>

</beans>
  • 所谓依赖指的是bean对象的创建依赖于Spring容器。bean对象依赖的资源。
  • 所谓注入指的是Bean对象所依赖的资源,由容器来设置和装配。

测试:

package com.muzili.pojo;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }
}

准备工作已就绪,可以运行程序了 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值