
Spring IoC(控制反转)、DI(依赖注入)
读取XML文件; 解析XML文件,生成Bean对象并存储在容器中(实际上是一个Map接口,key为XML中的id,value为实例); 通过id访问Bean对象。
发布日期:2021-05-08 17:19:58
浏览次数:21
分类:精选文章
本文共 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配置示例:
通过以上方法,可以轻松实现依赖注入,减少代码耦合度。
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年05月07日 01时35分53秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
MySQL错误1366处理方法
2019-03-16
VxWorks 操作系统学习笔记
2019-03-16
驱动程序之_1_字符设备_13_USB设备_1_基本概念
2019-03-16
微机原理 6-计算机中常用的数制
2019-03-16
window系统下安装使用curl命令工具
2019-03-16
假如计算机是中国人发明的,那代码应该这么写
2019-03-16
神器 Codelf !
2019-03-16
趣图:会算法和不会算法的区别
2019-03-16
区块链会2020再次爆发,先学点DAPP压压惊,跟我一起学《区块链DApp入门实战》
2019-03-16
问题解决28:微信网页授权出现redicet_uri 参数错误
2019-03-16
LeakCanary 中文使用说明
2019-03-16
反转链表,(5)
2019-03-16
Camera (api1)的打开过程
2019-03-16
wxwidgets绘图
2019-03-16
wxwidgets事件处理
2019-03-16
用OpenCv转换原始图像数据到wximage
2019-03-16
codeblocks下wxWidgets编译与配置
2019-03-16
OpenCv+wxwidgets尝试
2019-03-16
wxwidgets自定义事件+调试
2019-03-16
wxwidgets编写多线程程序--wxThread
2019-03-16