Spring学习3 - Bean的属性、DI依赖注入三种方式
文章目录
- 1. 属性设置 - XML配置文件、以及与其等价的注解形式
- 2. 依赖注入DI的方式
- 2.1 属性自动注入 - 不推荐
- 代码准备阶段
- 代码测试
- 2.2 子节点构造器注入 - 不推荐
- 2.3 子节点手动注入
- 2.3.1 Property不是多值类
- 2.3.1 是多值类 - Property需要子、孙节点
1. 属性设置 - XML配置文件、以及与其等价的注解形式
注意: - 所有Bean都懒加载,不过优先级比较低
2. 依赖注入DI的方式
即类一旦被初始化、类内的某些属性通过Spring容器的XML文件一同自动的初始化
2.1 属性自动注入 - 不推荐
通过Spring自带的Junit测试工具进行测试DI注入
代码准备阶段
EmpService3 - 通过自动注入empDao3自动的会被初始化-无须手动new
public class EmpService3 {EmpDao3 empDao3;EmpService3() {System.out.println("EmpService3被实例化");}EmpService3(EmpDao3 empDao) {System.out.println("EmpService3被实例化");}public void setEmpDao3(EmpDao3 empDao3) {this.empDao3 = empDao3;}}
EmpDao3
public class EmpDao3 {EmpDao3() {System.out.println("EmpDao3被实例化");}
}
Spring容器配置文件 - 注意共有6个类,只是上面两个的复制品,读者自行复制,改一下编号就可以
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <bean name="empDao" class="top.linruchang.dao.EmpDao" ></bean><bean name="empService" class="top.linruchang.service.EmpService" autowire="constructor"></bean><bean name="empDao2" class="top.linruchang.dao.EmpDao2" ></bean><bean name="empService2" class="top.linruchang.service.EmpService2" autowire="byType"></bean><bean name="empDao3" class="top.linruchang.dao.EmpDao3" ></bean><bean name="empService3" class="top.linruchang.service.EmpService3" autowire="byName"></bean></beans>
代码测试
@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext.xml")
public class TestSpringContainer {// 该注解的作用 - 自动初始化EmpService - 如果能自动注入,则对象实例化时,属性也会相应得到实例化(依赖注入)@Autowired EmpService es;@AutowiredEmpService2 es2;@AutowiredEmpService3 es3;@Testpublic void test1() {}}
运行结果
2.2 子节点构造器注入 - 不推荐
Spring容器的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 构造器参数类型 - type是被注入的Class全限定名 ref被注入的bean-name属性名 --><bean name="empDao" class="top.linruchang.dao.EmpDao" ></bean><bean name="empService" class="top.linruchang.service.EmpService"><constructor-arg type="top.linruchang.dao.EmpDao" ref="empDao"></constructor-arg></bean><!-- 构造器参数类型 - name构造器形参的变量名 ref被注入的bean-name属性名 --><bean name="empDao2" class="top.linruchang.dao.EmpDao2" ></bean><bean name="empService2" class="top.linruchang.service.EmpService2"><constructor-arg name="empDao233" ref="empDao2"></constructor-arg></bean> <!-- 构造器参数类型 - index构造器第几个形参 ref被注入的bean-name属性名 --><bean name="empDao3" class="top.linruchang.dao.EmpDao3" ></bean><bean name="empService3" class="top.linruchang.service.EmpService3"><constructor-arg index="0" ref="empDao3"></constructor-arg></bean></beans>
测试代码:
@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext.xml")
public class TestSpringContainer {@AutowiredEmpService es;@AutowiredEmpService2 es2;@AutowiredEmpService3 es3;@Testpublic void test1() {}}
运行效果
2.3 子节点手动注入
2.3.1 Property不是多值类
Spring容器配置文件
<bean name="empDao" class="top.linruchang.dao.EmpDao"></bean> <bean name="empService" class="top.linruchang.service.EmpService"><property name="empDao1" ref="empDao"></property> </bean><bean name="empDao2" class="top.linruchang.dao.EmpDao2"></bean> <bean name="empService2" class="top.linruchang.service.EmpService2"><property name="empDao2" ref="empDao2"></property></bean><bean name="empDao3" class="top.linruchang.dao.EmpDao3"></bean> <bean name="empService3" class="top.linruchang.service.EmpService3"><property name="empDao3" ref="empDao3"></property></bean>
由运行结果可知 - 运行成功
2.3.1 是多值类 - Property需要子、孙节点
Emp代码
public class Emp {List<String> hobbies1;Set<String> hobbies2;Map<Integer, String> hobbies3;Properties hobbies4;String[] hobbies5;public void setHobbies1(List<String> hobbies1) {this.hobbies1 = hobbies1;System.out.println("hobbies1" + hobbies1);}public void setHobbies2(Set<String> hobbies2) {this.hobbies2 = hobbies2;System.out.println("hobbies2" + hobbies2);}public void setHobbies3(Map<Integer, String> hobbies3) {this.hobbies3 = hobbies3;System.out.println("hobbies3" + hobbies3);}public void setHobbies4(Properties hobbies4) {this.hobbies4 = hobbies4;System.out.println("hobbies4" + hobbies4);}public void setHobbies5(String[] hobbies5) {this.hobbies5 = hobbies5;System.out.println("hobbies5" + Arrays.toString(hobbies5));}}
配置文件 - 注入的属性是List、Set、Map、数组、Properties( 其实也是Map )
<bean name="emp" class="top.linruchang.domain.Emp"><property name="hobbies1"><list><value>篮球</value><value>足球</value></list></property><property name="hobbies2"><set><value>篮球</value><value>足球</value></set></property><property name="hobbies3"><map><entry key="1" value="篮球"></entry><entry key="2" value="足球"></entry></map></property><property name="hobbies4"><props><prop key="1">篮球</prop><prop key="2">足球</prop></props></property><property name="hobbies5"><array><value>篮球</value><value>足球</value></array></property></bean>
Spring单元测试代码
@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext.xml")
public class DITest {@AutowiredEmp emp;@Testpublic void test() {System.out.println(emp);}}
运行结果
Spring学习3 - Bean的属性、DI依赖注入三种方式相关推荐
- Spring 依赖注入三种方式的实现,及循环依赖问题的解决(源码+XML配置)
搬砖啦,搬砖啦,这几天在看Spring相关的书,下面给大家分享一下这几天的心得与收获,Go Go Go! Spring支持两种依赖注入方式,分别是属性注入,构造函数注入.除此之外,Spring还支持工 ...
- spring依赖注入三种方式
首先,要学习Spring中的Bean的注入方式,就要先了解什么是依赖注入. 依赖注入是指:让调用类对某一接口的实现类的依赖关系由第三方注入,以此来消除调用类对某一接口实现类的依赖. Spring容器中 ...
- 22 springboot依赖注入三种方式
1 基于构造函数的依赖注入 Spring 基于构造函数的依赖注入_w3cschoolJ虽然当前有关Spring Framework(5.0.3)的文档仅定义了两种主要的注入类型,但实际上有三种 pub ...
- Spring-初识Spring框架-IOC控制反转(DI依赖注入)
---恢复内容开始--- IOC :控制反转 (DI:依赖注入) 使用ioc模式开发 实体类必须有无参构造方法 1.搭建Spring环境 下载jar http://maven.springframew ...
- 详解spring的IOC控制反转和DI依赖注入
转载 详解spring的IOC控制反转和DI依赖注入 2018-06-05 15:45:34 jiuqijack 阅读数 2945 文章标签: spring IOC控制反转 DI依赖注入 更多 分类专 ...
- Spring循环依赖的三种方式以及解决办法
Spring循环依赖的三种方式以及解决办法 [转]https://www.cnblogs.com/liuqing576598117/p/11227007.html 示例 https://github. ...
- (八)Spring之IOC控制反转、DI依赖注入介绍和使用(详解)
文章目录 前言 Spring Spring IOC 简介 Bean IOC 概述 IOC 本质理解 Spring IOC 应用 IOC xml装配 IOC 依赖注入 IOC Bean的作用域 IoC ...
- 三大框架之spring框架+IoC控制反转、DI依赖注入
三大框架:业务层框架Spring+IoC+DI 往期文章:jsp与cookie.重定向与RESTFul架构支持 下一章节: 持久层框架MyBatis 初识Spring框架 MyBatis 入门http ...
- Spring的IOC(控制反转)与DI(依赖注入)
Spring控制反转与依赖注入 文章目录 Spring控制反转与依赖注入 1. 控制反转(IOC) 2. 依赖注入(DI) 3. 总结 1. 控制反转(IOC) 控制反转(Inversion of C ...
最新文章
- java.util.concurrent.Callable 接口 源码
- 突发!Spring Cloud 爆高危漏洞。。赶紧修复!!
- 初学Java--计算器
- php自动断词,PHP自动分页、防止英文单词被截段、去除HTML代码
- java两字符串是否相等_Java与JavaScript中判断两字符串是否相等的区别
- 二叉树前序遍历python输出_Python 二叉树查找 前序 中序 后序遍历
- JAVASE阶段流程图
- 凭实力搞砸公司重大项目,老板看到直呼内行
- Linux进程管理:上帝视角看进程调度
- 使用Cygwin登录Raspberry PI
- 洛谷 P4093: bzoj 4553: [HEOI2016/TJOI2016]序列
- winXP系统如何打开剪贴板查看器
- 互联网这股裁员风,要吹到啥时候?
- Java输出字符串中的叠词_java中正则表达式的简单运用 | iamxiarui
- Linux4.14加密框架中的主要数据结构(1)—— struct crypto_alg(通用算法)
- 迅雷协议分析–多链接资源获取
- 新型冠状肺炎——这个不一般的春节
- java计算抛物线的标准方程_抛物线方程公式大全_抛物线的四种标准方程_抛物线公式_抛物线方程及图像_高中数学知识点总结网...
- 高斯消元——解线性方程组+球形空间产生器+开关问题
- 十进制与二进制的转换