之前讲到的名为"duke"的bean有一个私有成员变量beanBags代表这个杂技师bean的一次性能够抛出的最多的数量,Juggler有一个构造函数,构造函数的第一个参数(这里只有一个参数)beanBags是一个整型的值,用于传递给Juggler的私有成员变量beanBags。
构造器注入的方法是:在bean中添加一个constructor-arg(如果构造函数的参数有两个,那就添加两个constructor-arg)。
在spring-idol.xml中修改bean "duke"如下:

  <bean id="duke" class="com.moonlit.myspring.Juggler" >
<constructor-arg name="beanBags" value="15" />
</bean>

再次运行FirstBean程序,输出如下:

JUGGLING 15 BEANBAGS

可以看到通过构造器诸如已经把duke的beanBags改为了15。
构造函数中的参数可能不是一个基础类型的变量,而可能是一个变量,这个时候只要把constructor-arg的value改成ref即可,ref对应的值需要被声明称一个bean元素。
我们使用一个会唱歌的杂技师PoeticJuggler类来演示,PoeticJuggler继承自Juggler,它具有一个Poem类型的私有成员变量poem,代表他要朗诵的诗歌。
Poem类:

package com.moonlit.myspring;

public interface Poem {
void recite();
}

我们定义一首名为Sonnet29的类用于表示名为一首sonnet29的诗:http://shakespeare-online.com/sonnets/29.html
Sonnet29实现了Poem接口。

package com.moonlit.myspring;

public class Sonnet29 implements Poem {
private static String[] LINES = {
"When, in disgrace with fortune and men's eyes,",
"I all alone beweep my outcast state,",
"And trouble deaf heaven with my bootless cries,",
"And look upon myself, and curse my fate,",
"Wishing me like to one more rich in hope,",
"Featur'd like him, like him with friends possess'd,",
"Desiring this man's art and that man's scope,",
"With what I most enjoy contented least;",
"Yet in these thoughts myself almost despising,",
"Haply I think on thee, and then my state,",
"Like to the lark at break of day arising",
"From sullen earth, sings hymns at heaven's gate;",
"For thy sweet love remember'd such wealth brings",
"That then I scorn to change my state with kings.",
};
public Sonnet29() {
}
public void recite() {
for (String line : LINES)
System.out.println(line);
}
}

有了Poem和他的一个实现类Sonnet29之后,我们开始来写PoeticJuggler,他继承自Juggler并且有一个Poem类型私有成员变量poem。

package com.moonlit.myspring;

public class PoeticJuggler extends Juggler {
private Poem poem;
public PoeticJuggler(Poem poem) {
super();
this.poem = poem;
}
public PoeticJuggler(int beanBags, Poem poem) {
super(beanBags);
this.poem = poem;
}
public void perform() throws PerformanceException {
super.perform();
System.out.println("While reciting...");
poem.recite();
}
}

并且,我们需要在xml文件中声明Sonnet29和PoeticJuggler类对应的bean。

  <bean id="sonnet29" class="com.moonlit.myspring.Sonnet29" />
<bean id="poeticDuke" class="com.moonlit.myspring.PoeticJuggler">
<constructor-arg value="16" />
<constructor-arg ref="sonnet29" />
</bean>

可以看到,"poeticDuke"使用了两个constructor-arg来声明参数,第一个参数使用value,第二个参数使用ref,Sonnet29类型的bean--"connet29"。
使用测试程序查看效果:

package com.moonlit.practice;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.moonlit.myspring.PerformanceException;
import com.moonlit.myspring.Performer; public class PracticeConstructor {
public static void main(String[] args) throws PerformanceException {
ApplicationContext context = new ClassPathXmlApplicationContext(
"spring-idol.xml");
Performer performer = (Performer) context.getBean("poeticDuke");
performer.perform();
}
}

程序输出如下:

JUGGLING 16 BEANBAGS
While reciting...
When, in disgrace with fortune and men's eyes,
I all alone beweep my outcast state,
And trouble deaf heaven with my bootless cries,
And look upon myself, and curse my fate,
Wishing me like to one more rich in hope,
Featur'd like him, like him with friends possess'd,
Desiring this man's art and that man's scope,
With what I most enjoy contented least;
Yet in these thoughts myself almost despising,
Haply I think on thee, and then my state,
Like to the lark at break of day arising
From sullen earth, sings hymns at heaven's gate;
For thy sweet love remember'd such wealth brings
That then I scorn to change my state with kings.

理解:我们可以通过构造器注入来模拟构造函数传入的参数,通过constructor-arg value="XX"传递一个基本类型的参数XX,通过constructor-arg ref="XX"传递一个bean。

Spring学习笔记--构造器注入的更多相关文章

  1. Spring学习笔记--依赖注入

    依赖注入和控制反转:http://baitai.iteye.com/blog/792980出自李刚<轻量级 Java EE 企业应用实战> Java应用是一种典型的依赖型应用,它就是由一些 ...

  2. Spring 学习笔记 ----依赖注入

    依赖注入 有三种方式,本文只学习下属性注入. 属性注入       属性注入即通过 setXxx方法()注入Bean的属性值或依赖对象,由于属性注入方式具有可选择性和灵活性高的优点,因此属性注入方式是 ...

  3. Spring学习笔记(七)模拟实际开发过程的调用过程XML版-Setter方式注入

    模拟实际开发过程的调用过程XML版-Setter方式注入 源码获取github [TOC] 1.项目结构 2.jar包跟上个一样 3.重写set方法 UserServiceImpl.java 1234 ...

  4. Spring学习笔记之 Spring IOC容器(一)之 实例化容器,创建JavaBean对象,控制Bean实例化,setter方式注入,依赖属性的注入,自动装配功能实现自动属性注入

    本节主要内容:       1.实例化Spring容器示例    2.利用Spring容器创建JavaBean对象    3.如何控制Bean实例化    4.利用Spring实现bean属性sett ...

  5. 不错的Spring学习笔记(转)

    Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是s ...

  6. Spring学习笔记之依赖的注解(2)

    Spring学习笔记之依赖的注解(2) 1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Com ...

  7. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

  8. Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...

  9. 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回

    作者:ssslinppp      时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...

随机推荐

  1. 关于github上开源nineoldandroids兼容动画的笔记

    github上面有个样例非常好的列举了NineOldAndroids兼容动画的使用方法.下载了下来然后执行測试了下: 由于要引用nineoldandroids的项目,我这里直接把它的源码拷贝进去了,用 ...

  2. 窗口过程 Wndproc

    操作系统向应用程序发送一系列消息,如左键按下和左键抬起,应用程序将通过GetMessage等方法 Wndproc应用例子最终将消息提交到窗口过程(WndProc)指向一个应用程序定义的窗口过程的指针. ...

  3. FusionCharts JavaScript API - Functions 常用方法整理笔记

    FusionCharts JavaScript API - Functions Home > FusionCharts XT and JavaScript > API Reference  ...

  4. android学习日记01--综述

    开个博客,写点关于Android的知识,希望温故而知新吧! 一.总体框架 先上一张google提供官方的Android框架图: Android系统架构由5部分组成,分别是:Linux Kernel.A ...

  5. 为什么手机无法执行应用? Values之谜

    欢迎Follow我的GitHub, 关注我的CSDN, 精彩不断! CSDN: http://blog.csdn.net/caroline_wendy/article/details/68923156 ...

  6. IOS多线程之Block编程

    1 什么是block   iOS SDK 4.0開始,Apple引入了block这一特性.字面上说,block就是一个代码块.可是它的奇妙之处在于在内联(inline)运行的时候(这和C++非常像)还 ...

  7. 关于Cocos2d-x中GameController的定义

    GameController类是继承自Scheduler(有计时功能)或者Ref(可自动释放)的用户自己定义的一个控制器 如果一个场景有多个层的话,要用GameController,一个GameCon ...

  8. ubuntu16.04 安装caffe以及python接口

    http://blog.csdn.net/qq_25073253/article/details/72571714http://blog.csdn.net/greed7480/article/deta ...

  9. javascript删除字符串最后一个字符

    搜到很多无用的答案,这对新手来讲是很耗费时间的事情,我把正确的方法贴出来,以便有的新手不再踩这个坑. JavaScript slice() 方法 定义和用法 slice() 方法可提取字符串的某个部分 ...

  10. Lost connection to MySQL server at 'waiting for initial communication packet', system error: 0

    场景: 192.168.7.27 需要访问 192.168.7.175 上的MySQL数据库,连接时报错. 原因: MySQL的配置文件默认没有为远程连接配置好,需要更改下MySQL的配置文件. 解决 ...