文章目录

  • 1. 属性设置 - XML配置文件、以及与其等价的注解形式
  • 2. 依赖注入DI的方式
    • 2.1 属性自动注入 - 不推荐
      • 代码准备阶段
      • 代码测试
    • 2.2 子节点构造器注入 - 不推荐
    • 2.3 子节点手动注入
      • 2.3.1 Property不是多值类
      • 2.3.1 是多值类 - Property需要子、孙节点

1. 属性设置 - XML配置文件、以及与其等价的注解形式

注意: - 所有Bean都懒加载,不过优先级比较低

容器启动时
属性
lazy-init:true/false(default) - 懒加载
scope
singleton:单例
prototype:每次获取都会被创建新对象
request:每次请求都会被创建新对象
session:不同session获取会被创建新对象
init-method:创建对象时,被触发方法
destroy-method:对象被回收时,被触发的方法
Bean注解
@PostConstructor:等价于init-method属性
@PreDestory:等价于destory-method属性
@Scope('singleton'):等价于scope属性
@Compone:将Bean注册到Spring容器上

2. 依赖注入DI的方式

即类一旦被初始化、类内的某些属性通过Spring容器的XML文件一同自动的初始化

2.1 属性自动注入 - 不推荐

通过Spring自带的Junit测试工具进行测试DI注入

autowire属性值
default/no:不会自动注入
byType:根据setxx方法的参数类型进行自动注入
byName:根据setxx方法的方法名去掉set并且将首字母小写方法名 - 与一致
constructor:根据构造器的参数类型进行注入
代码准备阶段

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 子节点构造器注入 - 不推荐

的属性
index:构造器参数的位置,从0开始
name:构造器形参的变量名
type:根据构造器的形参类型
ref:被注入的bean的name属性名
value:可直接写字面变量的值

  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不是多值类
属性
name:调用setxxx() 去掉set并且将首字母小写
ref:bean的name或者id名


  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依赖注入三种方式相关推荐

  1. Spring 依赖注入三种方式的实现,及循环依赖问题的解决(源码+XML配置)

    搬砖啦,搬砖啦,这几天在看Spring相关的书,下面给大家分享一下这几天的心得与收获,Go Go Go! Spring支持两种依赖注入方式,分别是属性注入,构造函数注入.除此之外,Spring还支持工 ...

  2. spring依赖注入三种方式

    首先,要学习Spring中的Bean的注入方式,就要先了解什么是依赖注入. 依赖注入是指:让调用类对某一接口的实现类的依赖关系由第三方注入,以此来消除调用类对某一接口实现类的依赖. Spring容器中 ...

  3. 22 springboot依赖注入三种方式

    1 基于构造函数的依赖注入 Spring 基于构造函数的依赖注入_w3cschoolJ虽然当前有关Spring Framework(5.0.3)的文档仅定义了两种主要的注入类型,但实际上有三种 pub ...

  4. Spring-初识Spring框架-IOC控制反转(DI依赖注入)

    ---恢复内容开始--- IOC :控制反转 (DI:依赖注入) 使用ioc模式开发 实体类必须有无参构造方法 1.搭建Spring环境 下载jar http://maven.springframew ...

  5. 详解spring的IOC控制反转和DI依赖注入

    转载 详解spring的IOC控制反转和DI依赖注入 2018-06-05 15:45:34 jiuqijack 阅读数 2945 文章标签: spring IOC控制反转 DI依赖注入 更多 分类专 ...

  6. Spring循环依赖的三种方式以及解决办法

    Spring循环依赖的三种方式以及解决办法 [转]https://www.cnblogs.com/liuqing576598117/p/11227007.html 示例 https://github. ...

  7. (八)Spring之IOC控制反转、DI依赖注入介绍和使用(详解)

    文章目录 前言 Spring Spring IOC 简介 Bean IOC 概述 IOC 本质理解 Spring IOC 应用 IOC xml装配 IOC 依赖注入 IOC Bean的作用域 IoC ...

  8. 三大框架之spring框架+IoC控制反转、DI依赖注入

    三大框架:业务层框架Spring+IoC+DI 往期文章:jsp与cookie.重定向与RESTFul架构支持 下一章节: 持久层框架MyBatis 初识Spring框架 MyBatis 入门http ...

  9. Spring的IOC(控制反转)与DI(依赖注入)

    Spring控制反转与依赖注入 文章目录 Spring控制反转与依赖注入 1. 控制反转(IOC) 2. 依赖注入(DI) 3. 总结 1. 控制反转(IOC) 控制反转(Inversion of C ...

最新文章

  1. java.util.concurrent.Callable 接口 源码
  2. 突发!Spring Cloud 爆高危漏洞。。赶紧修复!!
  3. 初学Java--计算器
  4. php自动断词,PHP自动分页、防止英文单词被截段、去除HTML代码
  5. java两字符串是否相等_Java与JavaScript中判断两字符串是否相等的区别
  6. 二叉树前序遍历python输出_Python 二叉树查找 前序 中序 后序遍历
  7. JAVASE阶段流程图
  8. 凭实力搞砸公司重大项目,老板看到直呼内行
  9. Linux进程管理:上帝视角看进程调度
  10. 使用Cygwin登录Raspberry PI
  11. 洛谷 P4093: bzoj 4553: [HEOI2016/TJOI2016]序列
  12. winXP系统如何打开剪贴板查看器
  13. 互联网这股裁员风,要吹到啥时候?
  14. Java输出字符串中的叠词_java中正则表达式的简单运用 | iamxiarui
  15. Linux4.14加密框架中的主要数据结构(1)—— struct crypto_alg(通用算法)
  16. 迅雷协议分析–多链接资源获取
  17. 新型冠状肺炎——这个不一般的春节
  18. java计算抛物线的标准方程_抛物线方程公式大全_抛物线的四种标准方程_抛物线公式_抛物线方程及图像_高中数学知识点总结网...
  19. 高斯消元——解线性方程组+球形空间产生器+开关问题
  20. 十进制与二进制的转换

热门文章

  1. 机械制图——常见的机件表达
  2. 一名合格的电子工程师英文水平应该达到什么标准?
  3. python 操作npy文件
  4. 敏捷项目管理实战第三天 组织、计划、执行与监控
  5. 苹果承认部分iPhone12屏幕泛绿问题,对LGD是重大打击
  6. WIN10升级后,点击桌面图标无反应,鼠标滑到任务栏转圈,单击右键白屏闪烁。
  7. 数据分析笔试记录--广联达
  8. 赫尔德不等式证明闵可夫斯基不等式
  9. C语言增一减一运算符
  10. 原神服务端建模修改模型贴图(SpecialK)教程