我试图在Spring上下文文件中创建一个对象数组,这样我就可以将其注入到如下声明的构造函数中:
public RandomGeocodingService(GeocodingService... services) { }
我正在尝试使用<array>
标记:
<bean id="googleGeocodingService" class="geocoding.GoogleGeocodingService">
<constructor-arg ref="proxy" />
<constructor-arg value="" />
</bean>
<bean id="geocodingService" class="geocoding.RandomGeocodingService">
<constructor-arg>
<array value-type="geocoding.GeocodingService">
<!-- How do I reference the google geocoding service here? -->
</array>
</constructor-arg>
</bean>
我在文档中找不到关于如何做到这一点的示例或其他东西。另外,你有任何建议可以更好地实现我正在尝试做的事情,请让我知道:)。
发布于 2010-08-31 23:55:45
这是因为没有<array>
这样的东西,只有<list>
。
好消息是,Spring将根据需要在列表和数组之间自动转换,因此将数组定义为<list>
,Spring将强制将其转换为数组。
这应该是可行的:
<bean id="googleGeocodingService" class="geocoding.GoogleGeocodingService">
<constructor-arg ref="proxy" />
<constructor-arg value="" />
</bean>
<bean id="geocodingService" class="geocoding.RandomGeocodingService">
<constructor-arg>
<list>
<ref bean="googleGeocodingService"/>
</list>
</constructor-arg>
</bean>
如果需要,Spring还会将单个bean强制到一个列表中:
<bean id="geocodingService" class="geocoding.RandomGeocodingService">
<constructor-arg>
<ref bean="googleGeocodingService"/>
</constructor-arg>
</bean>
发布于 2012-09-27 22:21:34
Spring可以自动将列表转换为array[]
请查看http://forum.springsource.org/showthread.php?37767-Injecting-String-Array
<bean name="test" class="Test">
<property name="values" value="hugo,emil"></property>
</bean>
发布于 2010-09-01 00:30:51
查看util schema。
https://stackoverflow.com/questions/3610804
复制相似问题