一篇文章带你掌握主流基础框架——Spring( 八 )

  • 实际开发中根据情况分析 , 如果受控对象没有提供setter方法则只能采用构造器注入
  • 自己开发的模块尽量推荐setter注入
  • 依赖自动装配在前面我们学习了手动注入的方法 , 但Spring其实为我们提供了一种依赖自动装配的语法:
    • IoC容器根据bean所依赖的资源在容器中自动查找并注入bean中的过程称为自动装配
    自动装配方式:
    • 按类型(常用)
    • 按名称
    • 按构造方法
    • 不启用
    自动装配语法:
    <?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 http://www.springframework.org/schema/beans/spring-beans.xsd"><bean class="com.itheima.dao.impl.BookDaoImpl"/><!--autowire属性:开启自动装配 , 通常使用按类型装配--><bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="byType"/></beans>
    依赖自动装配特征:
    1. 自动装配用于引用类型注入 , 不能对简单类型进行操作
    2. 使用按类型装配时(byType)必须保障容器中相同类型的bean唯一 , 推荐使用
    3. 使用按名称装配时(byName)必须保障容器中具有指定名称的bean , 因变量名与配置耦合 , 不推荐使用
    4. 自动装配优先级低于setter注入和构造器注入 , 同时出现时 , 自动装配配置失效
    依赖集合注入除了基本类型和引入类型外 , 我们有时也需要注入集合
    下面我们简单介绍一下结合的注入:
    // 数据类 package com.itheima.dao.impl;import com.itheima.dao.BookDao;import java.util.*;public class BookDaoImpl implements BookDao {private int[] array;private List<String> list;private Set<String> set;private Map<String,String> map;private Properties properties;public void setArray(int[] array) {this.array = array;}public void setList(List<String> list) {this.list = list;}public void setSet(Set<String> set) {this.set = set;}public void setMap(Map<String, String> map) {this.map = map;}public void setProperties(Properties properties) {this.properties = properties;}public void save() {System.out.println("book dao save ...");System.out.println("遍历数组:" + Arrays.toString(array));System.out.println("遍历List" + list);System.out.println("遍历Set" + set);System.out.println("遍历Map" + map);System.out.println("遍历Properties" + properties);}}<!--xml注入--><?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 http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"><!--数组注入--><!--注意:name:对应实现类中的内部成员名称<>里的array等为固定词汇--><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><value>chuanzhihui</value></list></property><!--set集合注入--><property name="set"><set><value>itcast</value><value>itheima</value><value>boxuegu</value><value>boxuegu</value></set></property><!--map集合注入--><property name="map"><map><entry key="country" value="https://www.huyubaike.com/biancheng/china"/><entry key="province" value="https://www.huyubaike.com/biancheng/henan"/><entry key="city" value="https://www.huyubaike.com/biancheng/kaifeng"/></map></property><!--Properties注入--><property name="properties"><props><prop key="country">china</prop><prop key="province">henan</prop><prop key="city">kaifeng</prop></props></property></bean></beans>

    经验总结扩展阅读