3. DI: 依赖注入
2022年9月6日
3. DI: 依赖注入
在创建对象时给属性赋值
di 的实现语法:
- 在 spring 的配置文件中,使用标签和属性完成,叫做基于 XML 的 di 实现
- 使用 spring 中的注解,完成属性赋值, 叫做基于注解的 id 实现
di 的语法分类:
set 注入(设值注入): spring 调用类的 set 方法,在 set 方法可以实现属性的赋值
80%左右都是使用的 set 注入
构造注入,spring 调用类的有参构造方法,创建对象在构造方法中完成赋值
set 注入
- 简单类型的 set 注入
- 简单类型: spring 中规定 java 的基本数据类型和 String 都是简单类型
- Spring 会自动根据 set 方法的参数类型来改变 value 值的数据类型
<bean id="xx" class="yyy">
<property name="属性名字" value="此属性的值"/>
<!--一个property只能给一个属性赋值-->
<property..../>
</bean>
<bean id="student" class="com.demo.di01.Student">
<property name="name" value="小明"/>
<property name="age" value="1"/>
</bean>
- 引用类型的 set 注入
- 引用类型: spring 调用类的 set 方法
- 被注入的引用类型也必须在 xml 文件中声明
<bean id="xxx" class="yyy">
<property name="属性名称" ref="bean的id(对象的名称)" />
</bean>
<!--声明School对象-->
<bean id="school" class="com.demo.di01.School">
<property name="name" value="学校"/>
</bean>
<!--声明Student对象-->
<bean id="student" class="com.demo.di01.Student">
<property name="name" value="小明"/>
<property name="age" value="1"/>
<!--引用类型-->
<property name="school" ref="school"/>
</bean>
- 特殊的 set 注入
数组, list, map, null, 配置文件, set
- 数组
<!--数组-->
<property name="games">
<array>
<value>game1</value>
<value>game2</value>
<value>game3</value>
</array>
</property>
- list 集合
<!--list-->
<property name="hobby">
<list>
<value>hobby1</value>
<value>hobby2</value>
<value>hobby3</value>
<value>hobby4</value>
</list>
</property>
- map 集合
<!--map-->
<property name="card">
<map>
<entry key="map1" value="map1"/>
<entry key="map2" value="map2"/>
<entry key="map3" value="map3"/>
</map>
</property>
- 注入 null
<!--null-->
<property name="wife">
<null/>
</property>
- Properties 配置信息
<!--Properties 配置信息-->
<property name="info">
<props value-type="java.util.Properties">
<prop key="info1">info1</prop>
<prop key="info2">info2</prop>
<prop key="info3">info3</prop>
</props>
</property>
- set 集合
<!--set-->
<property name="books">
<set>
<value>book1</value>
<value>book2</value>
<value>book3</value>
<value>book4</value>
<value>book5</value>
</set>
</property>
构造注入
使用有参构造器
构造注入使用<constructor-arg>
标签
<constructor-arg>
标签: 一个<constructor-arg>
表示构造方法一个参数
<constructor-arg>
标签属性:
- name: 表示构造方法的形参名
- index: 表示构造方法的参数的位置,参数从左往右位置是 0,1,2 的顺序
- value: 构造方法的形参类型是简单类型的,使用 value
- ref: 构造方法的形参类型是引用类型的,使用 ref
- 下标赋值
<bean id="hello" class="pojo.Hello">
<constructor-arg index="0" value="HelloSpring"/>
</bean>
- 参数赋值
<bean id="hello" class="pojo.Hello">
<constructor-arg name="string" value="HelloSpring"/>
</bean>
- 类型赋值
不建议使用,多个同类型会按顺序执行可能传错
<bean id="hello" class="pojo.Hello">
<constructor-arg type="int" value="1"/>
<constructor-arg type="java.lang.String" value="HelloSpring"/>
</bean>
- 当不指定下标,属性,类型时,默认为下标赋值
<bean id="hello" class="pojo.Hello">
<constructor-arg value="1"/>
<constructor-arg value="HelloSpring"/>
</bean>
命名空间注入
p 命名空间
通过 set() get()方法
- 需要约束
xmlns:p="http://www.springframework.org/schema/p"
c 命名空间
通过构造方法
- 需要约束
xmlns:c="http://www.springframework.org/schema/c"
实体类:
@Data
public class User {
private String name;
private int age;
}
p 命名空间:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="pojo.User" p:name="name" p:age="18"/>
</beans>
c 命名空间:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="pojo.User" c:age="20" c:name="name"/>
</beans>