Spring DI:依赖注入全解析
Spring DI
- 什么是依赖注入(DI)
- Spring DI 的实现方式
- 构造函数注入(无复杂类型注入)
- 语法
- 属性
- 使用步骤
- Setter 方法注入
- 语法
- 属性
- 使用步骤
- 注解注入
- 属性注入
- Spring DI 支持的注入类型
- 基本类型以及字符串
- JavaBean类型
- 复杂类型
- Spring 容器与 Bean 管理
- Bean 的作用域(Singleton、Prototype 等)
- 总结
- Spring DI 的核心价值总结
什么是依赖注入(DI)
Spring DI(Dependency Injection,依赖注入)
Spring 框架的核心特性之一,它是控制反转(IoC,Inversion of Control)思想的具体实现方式。
其核心思想是:将对象之间的依赖关系交给容器来管理,而不是由对象自己创建或查找依赖对象。
Spring DI 的实现方式
在本篇文章中主要讲解构造器注入以及Setter方法注入
构造函数注入(无复杂类型注入)
语法
构造注入的语法:
<constructor-arg 属性名="属性值"></constructor-arg>
属性
name:参数名type:参数类型index:参数下标value:值ref:需要关联的类id
使用步骤
1.提供构造方法(在要注入的类中)
public class Studnet {private String name;private int age;//提供Student类的全参构造方法(用于构造注入)public Studnet(String name, int age) {this.name = name;this.age = age;}//提供Student类的无参构造方法public Studnet() {}//重写toString@Overridepublic String toString() {return "Studnet{" +"name='" + name + '\'' +", age=" + age +'}';}
}
2.配置标签(在配置文件中)
构造注入方式一(按照属性名注入):
<!--基本类型与String--><bean id="student1" class="com.ape.pojo.Student">
<constructor-arg name="name" value="xxy">
</constructor-arg><constructor-arg name="age" value="18"></constructor-arg></bean>
构造注入方式二(按照属性下标值注入):
<!--基本类型与String-->
<bean id="student2" class="com.ape.pojo.Studnet"><constructor-arg index="0" value="xxy"></constructor-arg><constructor-arg index="1" value="18"></constructor-arg></bean>
构造注入方式一(按照属性类型注入):
<!--基本类型与String--><bean id="student3" class="com.ape.pojo.Studnet"><constructor-arg type="java.lang.String" value="xxy"></constructor-arg><constructor-arg type="int" value="18"></constructor-arg></bean>
Setter 方法注入
语法
Setter注入的语法:
语法:<property name="属性名" ref="关联的id"></property>
属性
name:参数名value:值ref:需要关联的类id
使用步骤
1.提供Setter方法(在要注入的类中)
public class Studnet {private String name;private int age;//提供Student类的Setter方法(用于Setter注入)public void setName(String name) {this.name = name;}public void setAge(int age) {this.age= age;}//重写toString@Overridepublic String toString() {return "Studnet{" +"name='" + name + '\'' +", age=" + age +'}';}
}
2.配置标签(在配置文件中)
<!--基本类型与String--><bean id="student" class="com.ape.bean.Student"><property name="name" value="xxy"></property><property name="age" value="18"></property></bean>
注解注入
注入类
替换:<bean id="" class=""></bean>
位置:类
注意:@Component与<context:component-scan base-package=""></context:component-scan>必须联合使用@Component=====>可以注入所有类
@Repository=====>注入数据访问层
@Service========>注入业务层
@Controller=====>注入控制层
注入数据
@Value含义:注入基本数据替换:<property></property>修饰:成员变量或对应的set方法语法:@Value("数据内容")@Value("${动态获取}")注意: <context:property-placeholder location="classpath:msg.properties"></context:property-placeholder>与 @Value联合使用@Autowired语法:@Autowired(required = "true-默认、false、是否必须进行装配")修饰:成员变量含义:按照“类型装配”,有@Autowired后 构造方法/set方法可省略
属性注入
Spring DI 支持的注入类型
基本类型以及字符串
在构造注入以及Setter注入的语法演示实例中展示啦,就在上面呦⬆
JavaBean类型
构造注入JavaBean类型
创建StudentDao,StudentService,StudentController三个接口实现类
将他们注入容器并创建依赖关系
<bean id="studentDao" class="com.ape.dao.StudentDao"></bean><bean id="studentController" class="com.ape.controller.StudentController"><constructor-arg name="iStudentService" ref="studentService"></constructor-arg></bean><bean id="studentService" class="com.ape.service.StudentService"><constructor-arg name="iStudentDao" ref="studentDao"></constructor-arg>
</bean>
Setter注入JavaBean类型
创建StudentDao,StudentService,StudentController三个接口实现类
将他们注入容器并创建依赖关系
<bean id="studentDaoImp" class="com.ape.dao.StudentDaoImp"></bean><bean id="studentService" class="com.ape.service.StudentService">
<!-- <property name="在StudentService中创建的要关联类的属性名" ref="要关联的类id"></property>--><property name="iStudentDao" ref="studentDaoImp"></property></bean><bean id="studentController" class="com.ape.controller.StudentController">
<!-- <property name="在StudentController中创建的要关联类的属性名" ref="要关联的类id"></property>--><property name="iStudentService" ref="studentService"></property>
</bean>
复杂类型
Setter注入复杂类型
<bean id="teacher" class="com.ape.bean.Teacher"><property name="mylist"><list><value>list1</value><value>list2</value><value>list3</value><value>list4</value><value>list5</value></list></property><property name="myset"><set><value>set1</value><value>set2</value><value>set3</value><value>set4</value></set></property><property name="arrays"><array><value>array1</value><value>array2</value><value>array3</value></array></property><property name="mymap"><map><entry key="陕西" value="西安"></entry><entry key="河南" value="郑州"></entry><entry key="山西" value="太原"></entry></map></property><property name="prop"><props><prop key="propkey1">propvalue1</prop><prop key="propkey2">propvalue2</prop></props></property></bean>
Spring 容器与 Bean 管理
Bean 的作用域(Singleton、Prototype 等)
语法:<bean scope=""></bean>
属性值:singleton:单例,性能好,安全性差(默认)prototype:多例,性能差,安全性好request:一个请求创建一个对象session:一个session创建一个对象
<!-- 单例的使用 --><bean id="student" class="com.ape.pojo.Student" scope="singleton"></bean>
<!-- 多例的使用 --><bean id="student" class="com.ape.pojo.Student" scope="prototype"></bean>
总结
Spring DI 的核心价值总结
Spring DI(依赖注入)是 Spring 框架核心特性,
能有效解耦组件间依赖,避免类内直接创建依赖对象,使类间松耦合,方便维护与扩展;
提升代码可测试性,便于用 Mock 对象替代真实依赖进行单元测试;
增强代码可维护性与复用性,结构清晰易修改,解耦后组件更易复用;
借助多种配置方式,实现基于配置的灵活性,可动态调整组件依赖与行为;
还支持面向接口编程,契合依赖倒置原则,让系统更具扩展性与可替代性,助力构建灵活、可维护且易测试的应用程序