Spring—IOC、DI的一些简单使用方法说明

本文讲解了Spring5中的IoC(控制反转)和DI(依赖注入)概念,通过实例展示了如何使用XML配置文件解耦,以及如何在Student类中使用不同类型的注入,包括属性值、bean引用、数组、集合等。适合初学者理解Spring框架的基石。
摘要由CSDN通过智能技术生成

一路走来,如今已经开始学习Spring5了,不过还是个小菜鸟,此文章若有写的不好的地方大神勿喷,望指教。

今天学习的是IOC、DI

IOC

说明:

其意为:控制反转,以前传统的结构为DAO,Service,Servlet三层架构,但是不知道有没有发现,比如说:我在DAO层写了个A接口与A实现类,然后接下来我的Service层引用DAO层来调用A接口的方法,那么问题就在这里,如是用户要调用另外的类,那我是不是应该还要去Service层实现类去修改原来的代码,换个DAO层的引用,这样一来就要频繁调用其他DAO层的类。要么就要频繁更换Service实现类的调用DAO层的代码或是创建Service实现类。这样属于程序中的高耦合,不符合程序的设计规范(讲究高类聚,低耦合),每个层面之间都有互相依赖,影响全局。那么这种就是属于由业务层固定死的,我们要想解决这种病态,就可以将选择权交给用户,让用户去选择想要的功能,实现的方法或接口,这个时候就是要反转了(IOC),上代码!

 项目结构

Maven依赖

 此链接是对应上面项目结构的压缩包

spring-01-ioc1.rar-教育文档类资源-CSDN下载

或是直接去我博客的资源管理也可以看到

免费!!!!!!!!!!!!!!!!!!!


大家打开资源可以看到,这样就是使用了xml的方式来解耦合,只要修改xml的调用(ref)接口即可

,再无需改动源代码


DI

使用DI(依赖注入可以简单理解为:使用了xml后怎么为实体类设值,注入值)

 比如说,我可以先创建一个实体类

Address实体

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实体

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> game;

    private Properties infor;
    private String wife;

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

    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> getGame() {
        return game;
    }

    public void setGame(Set<String> game) {
        this.game = game;
    }

    public Properties getInfor() {
        return infor;
    }

    public void setInfor(Properties infor) {
        this.infor = infor;
    }

    public String getWife() {
        return wife;
    }

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

applicationContext.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="实体的全限定名">
        &lt;!&ndash;1.普通值注入:value&ndash;&gt;
        <property name="name" value="哈哈哈"/>
    </bean>-->

    <bean id="address" class="实体的全限定名">
        <property name="address" value="xian"></property>
    </bean>

    <bean id="student" class="实体的全限定名">
        <property name="name" value="呜呜"/>
        <!--<property name="name">
            <value>哈哈</value>
        </property>-->
        <!--2.bean注入:ref-->
        <property name="address" ref="address"/>

        <!--数组注入-->
        <property name="books">
            <array>
                <value>三国</value>
                <value>西游</value>
                <value>水浒</value>
            </array>
        </property>

        <!--list-->
        <property name="hobbies">
            <list>
                <value>eat</value>
                <value>drink</value>
                <value>play</value>
            </list>
        </property>

        <!--map-->
        <property name="card">
            <map>
                <entry key="1" value="12"/>
                <entry key="2" value="23"/>
            </map>
        </property>

        <!--set-->
        <property name="game">
            <set>
                <value>wangzhe</value>
                <value>daota</value>
                <value>lol</value>
            </set>
        </property>

        <property name="wife">
            <null></null>
        </property>

        <!--properties-->
        <property name="infor">
            <props>
                <prop key="id">20200405</prop>
                <prop key="姓名">hdk</prop>
            </props>
        </property>
    </bean>




</beans>

测试类test

import com.pojo.Student;
import com.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
        /*通过set方式的注入是最核心的,这就是依赖注入,一定要定义set方法!*/
    }

}

大家可以结合Student实体类的属性字段及类型来比较applicationContext.xml的类型注入(有注释),

这些就是常用类型的一些注入方式,一般用到set注入与构造器注入

构造器注入(这里为beans.xml)(在这里用的是有参构造,实体类必须提供无参构造!)

<bean id="AAA" class="实体的全限定名">
        <constructor-arg name="实体的属性" value="值"/>
</bean>

测试:

    @Test
    public void test02() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) context.getBean("AAA");
        /*有参调用*/
    }

User实体

public class User {
    private String name;

    public User() {
        System.out.println("无参构造!");
    }
    public  User(String name){
        System.out.println("user name="+name);
    }

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

    public String getName() {
        return name;
    }

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

若是有不懂的欢迎留言,不高冷

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

$初学者¥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值