Spring IoC(控制反转)、DI(依赖注入)
发布日期:2021-05-08 17:19:58 浏览次数:19 分类:精选文章

本文共 4233 字,大约阅读时间需要 14 分钟。

IoC与依赖注入(DI)入门

1. IoC(控制反转)

IoC(Inversion of Control,控制反转)是一种面向对象的设计思想,许多语言的框架都采用这种设计理念,尽管并非Spring独有。IoC的核心思想是将实例对象交给第三方容器管理,而不是在类内部直接创建实例。传统做法是类内直接初始化依赖对象,例如通过构造函数或属性直接赋值。

以下是一个传统的直接创建类的示例:

public class DateSource {    private String dataBaseUrl;    private String userName;    private String password;    public DateSource() {        // 不初始化属性    }    public DateSource(String dataBaseUrl, String userName, String password) {        this.dataBaseUrl = dataBaseUrl;        this.userName = userName;        this.password = password;    }}

使用这种方式,获取 DataSource 实例需要直接构造:

public class Main {    public static void main(String[] args) {        DataSource dataSource = new DataSource();    }}

IoC之后,类可以简化为只定义依赖关系:

public class DateSource {    private String dataBaseUrl;    private String userName;    private String password;    public DateSource(String dataBaseUrl, String userName, String password) {        this.dataBaseUrl = dataBaseUrl;        this.userName = userName;        this.password = password;    }}

在使用IoC时,通过配置文件(如XML)或其他方式将实例交给容器管理,其他地方只需注入所需依赖:

public class Main {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        DataSource dataSource = context.getBean("dataSource");    }}

2. 依赖注入(DI)

依赖注入(Dependency Injection,DI)是Spring实现IoC的一种方式。Spring支持三种主要的注入方式:构造函数注入、属性注入和接口注入。

2.1 构造函数注入

以下是一个使用构造函数注入的示例:

public class Service {    private Mapper mapper;    public Service(Mapper mapper) {        this.mapper = mapper;    }}

构造函数注入的使用方式如下:

public class Main {    public static void main(String[] args) {        Service service = new Service(new Mapper());    }}

2.2 属性注入

下面是一个使用属性注入的示例:

public class Service {    private Mapper mapper;    public void setMapper(Mapper mapper) {        this.mapper = mapper;    }}

属性注入的使用方式如下:

public class Main {    public static void main(String[] args) {        Service service = new Service();        service.setMapper(new Mapper());    }}

2.3 接口注入

下面是一个使用接口注入的示例:

public interface Service {    void init(Mapper mapper);}

实现接口并注入的方式如下:

public class ServiceImpl implements Service {    private Mapper mapper;    @Override    public void init(Mapper mapper) {        this.mapper = mapper;    }}

使用接口注入的方式如下:

public class Main {    public static void main(String[] args) {        Service service = new ServiceImpl();        service.init(new Mapper());    }}

2.4 不使用依赖注入的示例

不使用依赖注入时,类之间耦合严重:

public class Service {    private Mapper mapper;    public Service() {        this.mapper = new Mapper();    }}

这种耦合方式会导致代码难以维护和扩展。

通过以上示例可以看出,依赖注入是一个简单而有力的概念,可以有效减少代码耦合度。

3. Spring IoC中的依赖注入

3.1 Spring IoC中的依赖注入实例

Spring支持属性注入和构造注入两种方式来创建Bean实例。

以下是一个People类的使用示例:

package com.study.syrdbt.entity;public class People {    private String name;    private Integer age;    public People(String name, Integer age) {        this.name = name;        this.age = age;    }    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 "People{" +            "name='" + name + '\'' +             ", age=" + age +             '}';    }}

在XML配置文件中进行属性注入:

测试类如下:

package com.study.syrdbt;import com.study.syrdbt.entity.People;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        People syrdbt = (People) context.getBean("syrdbt");        System.out.println(syrdbt.toString());        People syrdbt1 = (People) context.getBean("syrdbt1");        System.out.println(syrdbt1.toString());    }}

3.2 Spring如何使用XML文件进行注入

  • 读取XML文件;
  • 解析XML文件,生成Bean对象并存储在容器中(实际上是一个Map接口,key为XML中的id,value为实例);
  • 通过id访问Bean对象。
  • Spring的内部实现远比上述流程复杂,但可以理解为通过反射创建Bean实例,并调用set方法注入设置的值。

    以下是一个简单的XML配置示例:

    通过以上方法,可以轻松实现依赖注入,减少代码耦合度。

    上一篇:(九)实现页面底部购物车的样式
    下一篇:(八)在vue中使用better-scroll,实现内容区域的滚动效果,以及右边与左边内容相对应,左边呈现高亮状态

    发表评论

    最新留言

    网站不错 人气很旺了 加油
    [***.192.178.218]2025年04月03日 05时19分57秒