Spring-Day3
8.依赖注入方式
setter注入-引用类型
在bean中定义引用类型属性并提供可访问的set方法
public class BookServiceImpl implements BookService { private BookDao bookDao; public void setBookDao(BookDao bookDao) { this.bookDao = bookDao; } }
配置中使用 properties 标签 ref 属性注入引用类型对象
<bean id=“bookService" class="com.itheima.service.impl.BookService.impl.BookServiceImpl"> <property name="bookDao" ref="bookDao"/> </bean> <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"?>
setter注入-简单类型
在 bean 中定义引用类型属性并提供可访问的 set 方法
public class BookDaoImpl implements BookDao { private int connectionNumber; public void setConnectionNumber(int connectionNumber) { this.connectionNumber = connectionNumber; } }
配置中使用 properties 标签 value 属性注入简单类型数据
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"> <properties name="conncetionNumber" value="10"/> </bean>
构造器注入-引用类型(了解)
在bean中定义引用类型属性并提供可访问的构造方法
public class BookServiceImpl implements BookService{ private BookDao bookDao; public BookServiceImpl(BookDao bookDao) { this.bookDao = bookDao; } }
配置中使用 constructor-arg 标签 ref 属性注入引用类型对象
<bean id="bookService" class="com.itheima.service.impl.BookDaoImpl"> <constructor-arg name="bookDao" ref="bookDao"/> </bean> <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"
构造器注入-简单类型(了解)
public class BookDaoImpl implements BookDao{ private connectionNumber; public setConncetionNumber(int conncetionNumber) { this.connectionNumber = connectionNumber; } }
配置中使用 constructor-arg 标签 value 属性注入简单数据类型
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"> <constructor-arg name="conncetionNumber" value="10"/> </bean>
构造器注入-参数配置(了解)
配置中使用 constructor-arg 标签 type 属性设置按形参类型注入
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"> <constructor-arg type="int" value="10"/> <constructor-arg type="java.lang.String" value="mysql"/> </bean>
配置中使用 constructor-arg 标签 index 属性设置按形参位置注入
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"> <constructor-arg index="0" value="10"/> <constructor-arg index="1" value="mysql"/> </bean>
依赖注入方式选择
-
强制依赖使用构造器,setter注入有概率不进行注入导致null对象出现
-
可选依赖使用setter注入进行,灵活性强
-
Spring框架倡导使用构造器,第三方框架内部大多数采用构造器注入的形式进行数据初始化
-
如果有必要可以两者同时使用,使用构造器注入完成强制依赖的注入,用setter注入完成可依赖的注入
-
实际开过程中要根据情况分析,如果受控对象没有提供setter方法就必须使用构造器注入
-
自己开发模块推荐使用setter注入
9.依赖自动装配
IoC容器根据bean所依赖的资源在容器中自动查找并注入到bean中的过程称为自动装配
配置中使用bean标签autowire属性设置自动装配的类型
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/> <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="byType"/>
依赖自动装配特征
-
自动装配用于引用类型依赖注入,不能对简单类型进行操作
-
使用按类型装配时必须保障容器中相同类型的bean唯一,推荐使用
-
使用按名称装配时必须保障容器中具有指定名称的bean,因变量名与配置耦合,不推荐使用
-
自动装配优先级低于setter注入与构造器注入,同时出现时,自动装配失效
10.集合注入
注入数组对象
<property name="array"> <array> <value>100</value> <value>200</value> <value>300</value> </array> </property>
注入list对象
<property name="list"> <list> <value>itcast</value> <value>itheima</value> <value>boxuegu</value> </list> </property>
注入set对象
<property name="set"> <set> <value>itcast</value> <value>itheima</value> <value>boxuegu</value> </set> </property>
注入Map对象
<property name="map"> <map> <entry key="country" value="china"/> <entry key="province" value="jiangsu"/> <entry key="city" value="wuxi"/> </map> </property>
注入Properties对象
<property name="properties"> <map> <prop key="country">china</prop> <prop key="province">jiangsu</prop> <prop key="city">wuxi</prop> </map> </property>