之前讲到的名为"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。