- 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>依赖自动装配特征:依赖集合注入除了基本类型和引入类型外 , 我们有时也需要注入集合
- 自动装配用于引用类型注入 , 不能对简单类型进行操作
- 使用按类型装配时(byType)必须保障容器中相同类型的bean唯一 , 推荐使用
- 使用按名称装配时(byName)必须保障容器中具有指定名称的bean , 因变量名与配置耦合 , 不推荐使用
- 自动装配优先级低于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>
经验总结扩展阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 手机指南针怎么看(指南针怎么看方向)
- 指南针怎么看方向(指南针罗盘)
- 指南针怎么看东南西北(指南针怎么看家里方位)
- 星姿妍彩妆是什么牌子的彩妆?
- 飞甩鸡毛是什么牌子?
- 爱琪梅花是什么电视剧中的人物?
- 晒斑和黄褐斑的区别是什么?
- 父母爱情安泰结局是什么?
- 晒黑了怎么快速变白?
- 飞乔是什么牌子?
