注入方式一:set注入

  1. <bean id="exampleBean" class="examples.ExampleBean">
  2.  
  3. <!-- setter injection using the nested <ref/> element -->
  4. <property name="beanOne"><ref bean="anotherExampleBean"/></property>
  5.  
  6. <!-- setter injection using the neater 'ref' attribute -->
  7. <property name="beanTwo" ref="yetAnotherBean"/>
  8. <property name="integerProperty" value="1"/>
  9. </bean>
  10.  
  11. <bean id="anotherExampleBean" class="examples.AnotherBean"/>
  12. <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
  1. public class ExampleBean {
  2.  
  3. private AnotherBean beanOne;
  4. private YetAnotherBean beanTwo;
  5. private int i;
  6.  
  7. public void setBeanOne(AnotherBean beanOne) {
  8. this.beanOne = beanOne;
  9. }
  10.  
  11. public void setBeanTwo(YetAnotherBean beanTwo) {
  12. this.beanTwo = beanTwo;
  13. }
  14.  
  15. public void setIntegerProperty(int i) {
  16. this.i = i;
  17. }
  18. }

注入方式二:构造注入

  1. <bean id="exampleBean" class="examples.ExampleBean">
  2.  
  3. <!-- constructor injection using the nested <ref/> element -->
  4. <constructor-arg>
  5. <ref bean="anotherExampleBean"/>
  6. </constructor-arg>
  7.  
  8. <!-- constructor injection using the neater 'ref' attribute -->
  9. <constructor-arg ref="yetAnotherBean"/>
  10.  
  11. <constructor-arg type="int" value="1"/>
  12. </bean>
  13.  
  14. <bean id="anotherExampleBean" class="examples.AnotherBean"/>
  15. <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
  1. public class ExampleBean {
  2.  
  3. private AnotherBean beanOne;
  4. private YetAnotherBean beanTwo;
  5. private int i;
  6.  
  7. public ExampleBean(
  8. AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
  9. this.beanOne = anotherBean;
  10. this.beanTwo = yetAnotherBean;
  11. this.i = i;
  12. }
  13. }

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

  1. <bean id="exampleBean" class="examples.ExampleBean"
  2. factory-method="createInstance">
  3. <constructor-arg ref="anotherExampleBean"/>
  4. <constructor-arg ref="yetAnotherBean"/>
  5. <constructor-arg value="1"/>
  6. </bean>
  7.  
  8. <bean id="anotherExampleBean" class="examples.AnotherBean"/>
  9. <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
  1. public class ExampleBean {
  2.  
  3. // a private constructor
  4. private ExampleBean(...) {
  5. ...
  6. }

  7. // a static factory method; the arguments to this method can be
  8. // considered the dependencies of the bean that is returned,
  9. // regardless of how those arguments are actually used.
  10. public static ExampleBean createInstance (
  11. AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
  12.  
  13. ExampleBean eb = new ExampleBean (...);
  14. // some other operations...
  15. return eb;
  16. }
  17. }

注入方式四:自动装配

小菜鸟学 Spring-Dependency injection(二)的更多相关文章

  1. 小代学Spring Boot之数据源

    想要获取更多文章可以访问我的博客 - 代码无止境. 经过一天对Spring Boot的研究,小代同学已经对Spring Boot框架有了一个大概的认识.并且还创建了一个简单的Spring Boot的W ...

  2. 小代学Spring Boot之集成MyBatis

    想要获取更多文章可以访问我的博客 - 代码无止境. 上一篇小代同学在Spring Boot项目中配置了数据源,但是通常来讲我们访问数据库都会通过一个ORM框架,很少会直接使用JDBC来执行数据库操作的 ...

  3. 小代学Spring Boot之自定义Starter

    想要获取更多文章可以访问我的博客 - 代码无止境. 上一篇小代同学在Spring Boot项目中配置了数据源,但是通常来讲我们访问数据库都会通过一个ORM框架,很少会直接使用JDBC来执行数据库操作的 ...

  4. Spring Dependency Injection浅析

    Dependency Injection 依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件. 1.在UserService中提供一个get/set的name方法, ...

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

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

  6. 小菜鸟 学MQ(二)

    mq服务启动以后 接着要做的事情就是 [发送]和[接受]消息. 首先有两种不同类型的Message:Topic,Queue 第一种Topic JMS规范定义了,Topic需要实现 发布和订阅两个功能, ...

  7. 小代学Spring Boot之开篇

    想要获取更多文章可以访问我的博客 - 代码无止境. 前情提要 小代是一名入职不久的程序员,公司同事都亲切的称他小代.有一天小代的老大陈BOSS和小代说,公司后端最近准备换技术框架了. 小代: 换成啥? ...

  8. 小菜鸟学Spring-读取属性文件值(三)

    Example: the PropertyPlaceholderConfigurer 属性配置文件内容如下所示: jdbc.driverClassName=org.hsqldb.jdbcDriver ...

  9. 小菜鸟学 Spring-bean scope (一)

    this information below just for study record of mine. 默认情况下:Spring 创建singleton bean 以便于错误能够被发现. 延迟加载 ...

随机推荐

  1. UESTC 881 神秘绑架案 --二维DP

    LRJ黑书上的例题. 化简均方差公式: 均值的平方一定,所以只需让矩形的总分的平方和最小即可. 定义:dp[k][x1][y1][x2][y2],以(x1,y1)为左上角坐标,(x2,y2)为右下角坐 ...

  2. HDU 5033 Building --离线+单调栈

    题意:给一些建筑物,x表示横坐标,h表示高度,然后查询某些坐标x,问从该点看向天空的最大张角是多大. 解法:离线操作,读入所有数据,然后按x升序排序,对每一个查询的x,先从左到右,依次添加x坐标小于x ...

  3. Android组件---四大布局的属性详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4372222.html Android常见布局有下面几种: LinearL ...

  4. Update和LateUpdate的区别

    LateUpdate晚于所有Update执行 在圣典里LateUpdate被解释成一句话:LateUpdate是在所有Update函数调用后被调用.这可用于调整脚本执行顺序. 当物体在Update里移 ...

  5. 关于tcpdump抓包一个很详细的介绍

    http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html

  6. 工厂模式(Factory Patter)

    1.工厂模式简介 工厂模式属于创建型模式,是专门用来创建对象的模式,抽象了实例化的过程.工厂模式分为 : 工厂方法模式.抽象工厂模式. 在学习工厂方法模式.抽象工厂之前,首先先要了解一下简单工厂模式, ...

  7. [原创]CI持续集成系统环境--Gitlab+Gerrit+Jenkins完整对接

    近年来,由于开源项目.社区的活跃热度大增,进而引来持续集成(CI)系统的诞生,也越发的听到更多的人在说协同开发.敏捷开发.迭代开发.持续集成和单元测试这些拉风的术语.然而,大都是仅仅听到在说而已,国内 ...

  8. 17SpringMvc_在业务控制方法中写入包装User的模型来收集参数——解决问题

    在解决问题之前,我要说明一下jsp页面上填入信息,一个用户的信息比如用户的名字,用户的电话,用户的手机等等,在这个jsp页面上填好信息后,转到有个action处理这个信息.原理是什么? 在jsp页面上 ...

  9. 思科简单教程CCNA

    这是CCNA的内容,从PC配置交换机(或者路由器),这里呢我们使用的软件叫pack 这是ciso开发的一款工具,能生动形象的模拟现实生活中组网技术的过程,下面我大概讲一下流程,想更多的了解我会录制一些 ...

  10. Discuz! X的CSS加载机制

    首先,每个页面都会加载以下两个css,data/cache/style_1_common.css和data/cache/style_1_forum_index.css.先讲讲这两个文件名的命名规则:第 ...