注入方式一:set注入

<bean id="exampleBean" class="examples.ExampleBean"><!-- setter injection using the nested <ref/> element -->
<property name="beanOne"><ref bean="anotherExampleBean"/></property><!-- setter injection using the neater 'ref' attribute -->
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
</bean><bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {private AnotherBean beanOne;private YetAnotherBean beanTwo;private int i;public void setBeanOne(AnotherBean beanOne) {this.beanOne = beanOne;}public void setBeanTwo(YetAnotherBean beanTwo) {this.beanTwo = beanTwo;}public void setIntegerProperty(int i) {this.i = i;}
}

注入方式二:构造注入

<bean id="exampleBean" class="examples.ExampleBean"><!-- constructor injection using the nested <ref/> element -->
<constructor-arg><ref bean="anotherExampleBean"/>
</constructor-arg><!-- constructor injection using the neater 'ref' attribute -->
<constructor-arg ref="yetAnotherBean"/><constructor-arg type="int" value="1"/>
</bean><bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {private AnotherBean beanOne;private YetAnotherBean beanTwo;private int i;public ExampleBean(AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {this.beanOne = anotherBean;this.beanTwo = yetAnotherBean;this.i = i;}
}

注入方式三:静态工厂方法注入

<bean id="exampleBean" class="examples.ExampleBean"factory-method="createInstance">
<constructor-arg ref="anotherExampleBean"/>
<constructor-arg ref="yetAnotherBean"/>
<constructor-arg value="1"/>
</bean><bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {// a private constructorprivate ExampleBean(...) {...}// a static factory method; the arguments to this method can be// considered the dependencies of the bean that is returned,// regardless of how those arguments are actually used.public static ExampleBean createInstance (AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {ExampleBean eb = new ExampleBean (...);// some other operations...return eb;}
}

注入方式四:自动装配

转载于:https://www.cnblogs.com/mengjianzhou/p/5986841.html

小菜鸟学 Spring-Dependency injection(二)相关推荐

  1. 一步一步学Spring Boot(二)课程发布了~~~

    课程名称 <一步一步学Spring Boot(二)> 学习地址 CSDN学习地址: http://edu.csdn.net/lecturer/994 51CTO学习地址:http://ed ...

  2. 菜鸟学SSH(十二)——Hibernate与Spring配合生成表结构

    前几天向大家介绍了一种用工具类生成数据表的方法,不过之前的方法需要使用一个跟项目关系不大的工具类.不免让人觉得有些多余,所以呢,今天再向大家介绍一种方法.即Hibernate与Spring配合生成表结 ...

  3. 小菜鸟学Python记

    菜鸟原本是做城市规划的,对城市空间的大数据分析一直有所向往 后来参与城市更新政策研究,也需要通过数据手段去收集行业信息和分析人群行为 2019年前后,听闻有个Python技术很厉害,可以爬取全网的信息 ...

  4. 菜鸟学算法——动态规划(二)

    概述 动态规划(dynamic programming)是运筹学的一个分支,是求解决策过程(decision process)最优化的数学方法,它是应用数学中用于解决某类最优化问题的重要工具.20世纪 ...

  5. 【小沐学qt】生成二维码

    目录 一.功能简介 二.本地生成二维码 1.第三方库Libqrencode 2.编写Qt程序代码,生成本地二维码 三.在线生成二维码 1.浏览器chrome 2.Google API 3.搜狐视频 A ...

  6. java做一个mud_菜鸟学Java(十二)——搭建一个完整的Java开发环境

    作为一个Java程序员,配置一个java开发环境是必备的技能,今天给广大菜鸟初学者补上一课.环境的配置,大概就分三个1,JDK 2,Tomcat(或者其他的)3,eclipse(或者myeclipse ...

  7. 小菜鸟学浏览器之感悟

    十年生死两茫茫, 不思量,自难忘. 千里孤坟,无处话凄凉. 纵使相逢应不识, 尘满面,鬓如霜. 夜来幽梦忽还乡, 小轩窗,正梳妆. 相顾无言,唯有泪千行. 料得年年断肠处, 明月夜,短松岗. 清明节, ...

  8. 【小沐学python】(二)Python常见问题汇总

  9. Spring IoC容器和Dependency Injection模式

    轻松学习Spring<一> IoC容器和Dependency Injection模式 最近公司需要,项目中要用到Spring和Ibatis.趁着过年好好学习学习.Ibatis就如同Hibe ...

最新文章

  1. 009_Gson版本支持
  2. SPIDR - 完美分割用户故事的五种简单技巧
  3. 12行代码AC——UVa 151 - Power Crisis(约瑟夫环)
  4. Python之路【第一篇】:环境搭建
  5. java什么变量用作英文_Java基础之变量-什么是变量
  6. Java中如何使用非阻塞异步编程——CompletableFuture
  7. backtype.storm.generated.InvalidTopologyException:null问题的解决
  8. 图解TCPIP-TCP IP
  9. JAVA Useful Program(1)
  10. Android7 mediacodec SEI
  11. 去掉serialVersionUID的警告
  12. Windows驱动签名,还需要使用EV代码签名证书吗?
  13. 如何将exe文件在linux下执行,如何轻松的在Linux中运行Windows的.exe文件
  14. 用MATLAB 读写各种文件 ∈ Matlab 使用笔记
  15. 如何在JUnit5中使用Mockito
  16. Excel单元格设置选择项
  17. LTE中的SRB--承载
  18. 树莓派CM4基于emmc安装Ubuntu系统及初始配置
  19. python美化excel_Python 使用 prettytable 库打印表格(美化输出)
  20. 2021.05.05青蛙过河

热门文章

  1. https ssl 总结
  2. 用ipad维护Linux服务器
  3. OSChina 周一乱弹 ——渴望咪咪还是渴望力量,都能给你
  4. 【算法和数据结构】_13_小算法_双链表
  5. 【逆天的算法】这几首宋词,你能看出来是出自计算机之手吗?
  6. Java 开源分布式缓存框架Ehcache
  7. Android UI(继承控件)--PopupWindow设置动画
  8. 想非常牛掰的在WORD中调中多级符号吗
  9. 443 ERROR [main] client.ConnectionManager$HConnectionImplementation: Can‘t get connection to ZooKeep
  10. [AI开发]目标跟踪之行为分析