写在前面😘
大一电子信息工程新生,请多多关照,希望能在 InfoQ 社区记录自己的学习历程!
【Spring 学习笔记】 系列教程基于 Spring 5.2.10.RELEASE讲解
一、Spring Bean 集合注入
在【Spring学习笔记(三)】已经讲了怎么注入基本数据类型和引用数据类型,接下来介绍如何注入比较特殊的数据类型——集合
1、集合常用标签
集合注入,用法也很简单,只需要在 Bean 标签下的 <property> 或<constructor-arg>元素中添加以下集合的标签,再通过 value 或者 ref 进行属性注入
注意: List 的底层也是通过数组实现的,所以<list>和<array>标签是可以混用
2、案例
创建一个 Collection 类和 Dept 类,代码如下👇
/*Collection类*/public class Collection { //1.数组 private int[] array; //2.list集合 private List<Dept> list; //3。set集合 private Set<Dept> set; //4.map集合 private Map<String, String> map; //5.properties集合 private Properties properties; //打印各个集合 public void fmt() { System.out.println("打印array:" + Arrays.toString(array)); System.out.println("打印List:" + list); System.out.println("打印Set:" + set); System.out.println("打印Map:" + map); System.out.println("打印Properties:" + properties); } -----省略setter方法----- }/*Dept类*/public class Dept { //部门编号 private int deptNo; //部门名称 private String deptName; @Override public String toString() { return "Dept{" + "deptNo=" + deptNo + ", deptName='" + deptName + '\'' + '}'; } -----省略setter方法----- }
复制代码
编写 spring 配置文件
<!--部门类属性注入--><bean id="dept" class="com.bighorn.pojo.Dept"> <property name="deptNo" value="1"/> <property name="deptName" value="后端组"/></bean>
<!--Collection类属性注入--><bean id="collection" class="com.bighorn.pojo.Collection"> <!--数组类型--> <property name="array"> <array> <value>114514</value> <value>114514</value> <value>114514</value> </array> </property> <!--List 类型--> <property name="list"> <list> <ref bean="dept"/> <ref bean="dept"/> <ref bean="dept"/> </list> </property> <!--Set 类型--> <property name="set"> <set> <ref bean="dept"/> <ref bean="dept"/> <ref bean="dept"/> </set> </property> <!--Map 类型--> <property name="map"> <map> <entry key="name" value="bighorn"/> <entry key="age" value="18"/> <entry key="sex" value="man"/> </map> </property> <!--Properties 类型--> <property name="properties"> <props> <prop key="username">root</prop> <prop key="password">123456</prop> </props> </property></bean>
复制代码
编写运行程序
public static void main(String[] args) { //获取IoC容器 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //从容器中获取对象 Collection collection = context.getBean("collection", Collection.class); //调用方法打印各个集合 collection.fmt();}
复制代码
运行结果如下👇
二、Spring Bean 自动装配
Spring 在 Bean 与 Bean 之间建立依赖关系的行为称为 “装配”
Spring Bean 属性注入 前面学习了,在 spring 配置文件中通过 <constructor-arg>和 <property> 中的 ref 属性,手动维护 Bean 与 Bean 之间的依赖关系的,即手动装配
1、什么是自动装配
如果容器中 Bean 很多,依赖关系又非常复杂,此时手动装配就略显吃力且繁琐了,所以 Spring 框架为我们提供了 “自动装配” 功能,提高开发效率。
Spring 自动装配:Spring IoC容器根据自动装配方式,为指定的 Bean 从应用上下文(AppplicationContext 容器)自动查找并自动注入它所依赖的 Bean。
2、自动装配的方式
Spring 默认不支持自动装配的。使用自动装配,则需要对 Spring XML 配置文件中 <bean> 元素的 autowire 属性进行设置,此时就不需要 ref 属性了。
3、案例
创建员工类 Employee 和部门类 Dept,添加各个成员变量的 setter 方法
/*员工类Employee*/public class Employee { //员工编号 private String empNo; //员工姓名 private String empName; //部门信息 private Dept dept; //各个属性的setter方法 public void setEmpNo(int empNo) { this.empNo = empNo; } public void setEmpName(String empName) { this.empName = empName; } public void setDept(Dept dept) { this.dept = dept; } ----省略toString方法------ }
/*部门类Dept*/public class Dept { //部门编号 private String deptNo; //部门名称 private String deptName; //各个属性的setter方法 public void setDeptNo(int deptNo) { this.deptNo = deptNo; } public void setDeptName(String deptName) { this.deptName = deptName; } ----省略toString方法------}
复制代码
编写 spring 配置文件
<!--部门类属性注入--><bean id="dept" class="com.bighorn.pojo.Dept"> <property name="deptNo" value="2"/> <property name="deptName" value="前端组"/></bean><!--员工类属性注入--><bean id="employee" class="com.bighorn.pojo.Employee" autowire="byType"> <property name="empNo" value="114514"/> <property name="empName" value="lzh"/></bean>
复制代码
编写运行程序
public static void main(String[] args) { //获取IoC容器 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //从容器中获取对象 Employee employee = context.getBean("employee", Employee.class); System.out.println(employee);}
复制代码
结果如下图👇
4、注意点
自动装配用于引用类型依赖注入,不能对简单类型进行操作
使用按类型装配时(byType)必须保障容器中相同类型的 bean 唯一,推荐使用
使用按名称装配时(byName)必须保障容器中具有指定名称的 bean,因变量名与配置耦合,不推荐使用
自动装配优先级低于 setter 注入与构造器注入,同时出现时自动装配配置失效
写在后面🍻
感谢观看啦✨
有什么不足,欢迎指出哦💖
评论