DI依赖注入
功能
在IoC容器中将不同层之间bean与bean进行关系绑定。
方式
setter注入
要求实体类在操作以前必须得有
setter
方法
;
代码实现:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="u" class="cn.ry.pojo.User">
<property name="id" value="100"></property>
<property name="name" value="张三"></property>
</bean>
</beans>
- 引用类型
- 简单类型
bean中定义引用类型属性并提供可访问的set方法—>配置中使用property标签中的value属性注入简单类型数
构造器注入
- 引用类型
- 简单类型
Bean中定义引用类型属性并提供可访问的构造方法->配置中使用constructor-arg标签ref属性注入引用类型对象。
配置中使用constructor-arg标签type属性设置按形参类型注入
配置中使用constructor-arg标签index属性设置按形参位置注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="u" class="cn.ry.pojo.User">
<!--
index:第几个参数 从0开始;
name: 参数名字:
ref: 自定义类型的引用;引用的是bean的id或者name;
type: 参数的类型,联合index一起使用;
value: 简单类型的值;
-->
<constructor-arg index="0" type="java.lang.Integer" value="1">
</constructor-arg>
<constructor-arg name="name" value="李四"></constructor-arg>
</bean>
</beans>
集合类型注入(使用较少)
- set集合注入
- list注入
- map注入
- array注入
- properties注入
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="r" class="cn.ry.pojo.Role" p:id="2" p:name="管理员"></bean>
<bean id="u" class="cn.ry.pojo.User" >
注解配置
@Value
属性、方法、参数级别的注解,用来直接给属性进行赋值操作。所操作的类型是简单数据类型。方法级
别的注解的时候需要加在setter方法上面。
@Resource
属性、方法、类级别的注解,用来自定义类型注入;
用法:@Resource(name=""|type=自定义类型的类对象);
此处的name的值存放的是交给spring容器的bean的name或者id,如果name="值"和beanName
不一致,会报错;
如果按照类型注入,在spring容器中,同种类型的bean不能出现多个;
如果被注入的属性上直接写了@Resource,会自动按照类型进行查找注入;
<property name="id" value="1"></property>
<property name="name" value="用户1"></property>
<property name="role" ref="r"></property>
<!-- set类型注入 -->
<property name="set">
<set>
<value>1</value>
<value>小林</value>
</set>
</property>
<!-- list类型注入 -->
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
</list>
</property>
<!-- map类型注入 -->
<property name="map">
<map>
<entry key="k1" value="m1"></entry>
<entry key="k2" value="m2"></entry>
</map>
</property>
<!-- 数组类型注入 -->
<property name="array">
<array>
<value>array1</value>
<value>array2</value>
</array>
</property>
<!-- Properties注入 -->
<property name="pro">
<props>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
</beans>
依赖注入方式选择
- 强制依赖使用构造器进行,使用setter注入有概率不进行注入导致nu11对象出现
- 可选依赖使用setter注入进行,灵活性强
- Spring框架倡导使用构造器,第三方框架内部大多数采用构造器注入的形式进行数据初始化,相对严谨
- 如果有必要可以两者同时使用,使用构造器注入完成强制依赖的注入,使用setter注入完成可选依赖的注入
- 实际开发过程中还要根据实际情况分析,如果受控对象没有提供setter方法就必须使用构造器注入
- 自己开发的模块推荐使用setter注入
注解配置
@Value
属性、方法、参数级别的注解,用来直接给属性进行赋值操作。所操作的类型是简单数据类型。方法级别的注解的时候需要加在 setter 方法上面。
@Resource
- 属性、方法、类级别的注解,用来自定义类型注入;
- 用法:@Resource(name=""|type=自定义类型的类对象);
- 此处的name的值存放的是交给spring容器的bean的name或者id,如果name="值"和beanName不一致,会报错;
- 如果按照类型注入,在spring容器中,同种类型的bean不能出现多个;
- 如果被注入的属性上直接写了@Resource,会自动按照类型进行查找注入;
@Autowired
- 构造方法、方法、属性、参数级别的注解,同于自定义类型注入;
- 不要求属性必须有setter方法;
- 要求spring容器中同种类型的bean只能有一个;
- 该注解中只有boolean类型的required,是否必须默认是true; 是否必须指的是——》是否强制注入;如果是false的话,当前值是null,也不会报错。
@Qualifier
组合注解 ,可以搭配@Autowired一块使用,实现两个注解同时修饰一个类。
@Autowired@Qualifier ( "dao2" )private UserDao dao ;
总结
- 简单数据类型和自定义数据类型使用注解方式进行注入,集合数据类型xml方式注入;
- 简单数据类型使用@Value注入即可;
- 自定义数据类型推荐使用@Autowired注入;