
Spring IoC(控制反转)、DI(依赖注入)
读取XML文件; 解析XML文件,生成Bean对象并存储在容器中(实际上是一个Map接口,key为XML中的id,value为实例); 通过id访问Bean对象。
发布日期: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文件进行注入
Spring的内部实现远比上述流程复杂,但可以理解为通过反射创建Bean实例,并调用set方法注入设置的值。
以下是一个简单的XML配置示例:
通过以上方法,可以轻松实现依赖注入,减少代码耦合度。
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2025年04月03日 05时19分57秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Jupyter Notebook 暗色自定义主题
2019-03-06
[Python学习笔记]组织文件
2019-03-06
基于Redo Log和Undo Log的MySQL崩溃恢复流程
2019-03-06
从RocketMQ的Broker源码层面验证一下这两个点
2019-03-06
如何正确的在项目中接入微信JS-SDK
2019-03-06
纵览全局的框框——智慧搜索
2019-03-06
快服务流量之争:如何在快服务中占领一席之地
2019-03-06
【活动】直播揭秘<如何从0开发HarmonyOS硬件>
2019-03-06
Unity平台 | 快速集成华为性能管理服务
2019-03-06
对模拟器虚假设备识别能力提升15%!每日清理大师App集成系统完整性检测
2019-03-06
使用Power BI构建数据仓库与BI方案
2019-03-06
Django认证系统并不鸡肋反而很重要
2019-03-06
tep用户手册帮你从unittest过渡到pytest
2019-03-06
12张图打开JMeter体系结构全局视角
2019-03-06
Spring Boot 2.x基础教程:构建RESTful API与单元测试
2019-03-06
[UWP 自定义控件]了解模板化控件(1):基础知识
2019-03-06
UWP 自定义控件:了解模板化控件 系列文章
2019-03-06
[UWP]从头开始创建并发布一个番茄钟
2019-03-06
WinUI 3 Preview 3 发布了,再一次试试它的性能
2019-03-06
使用命令把SpringBoot项目打包成可运行的jar包(简洁,操作性强)
2019-03-06