我正在阅读“Spring Recipes”一书,并尝试通过示例来研究 Spring“魔法”。这就是我所拥有的。Bean 类SequenceGenerator:
public class SequenceGenerator {
private List<Object> suffixes;
//.....
public void setSuffixes(List<Object> suffixes) {
this.suffixes = suffixes;
}
public synchronized String getSequence() {
StringBuffer buffer = new StringBuffer();
for (Object suffix : suffixes) {
buffer.append(suffix);
buffer.append("-");
}
return buffer.toString();
}
}
主类:
public class Main {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans.xml");
SequenceGenerator generator =
(SequenceGenerator) context.getBean("sequenceGeneratorSet");
System.out.println(generator.getSequence());
}
}
xml配置:
<bean id="sequenceGeneratorSet" class="com.apress.springrecipes.sequence.SequenceGenerator">
<property name="initial" value="100000"/>
<property name="suffixes">
<set>
<value>A</value>
<value>A</value>
<bean class="java.net.URL">
<constructor-arg value="http" />
<constructor-arg value="www.apress.com" />
<constructor-arg value="/" />
</bean>
</set>
</property>
</bean>
在 xml 中,我有意使用标签来观察结果。我也写了两次相同的值“A”。我注意到 Spring 为“后缀”属性注入了 ArrayList,作为 bean 类中的类型定义。但它的行为类似于输出设置,仅包含一个“A”值。有人知道Spring如何在内部解决这个问题吗?