首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Spring bean上下文中声明对象数组

在Spring bean上下文中声明对象数组
EN

Stack Overflow用户
提问于 2010-08-31 23:53:26
回答 4查看 42.5K关注 0票数 20

我试图在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>

我在文档中找不到关于如何做到这一点的示例或其他东西。另外,你有任何建议可以更好地实现我正在尝试做的事情,请让我知道:)。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 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>
票数 33
EN

Stack Overflow用户

发布于 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>
票数 8
EN

Stack Overflow用户

发布于 2010-09-01 00:30:51

查看util schema

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3610804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档