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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3610804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:38:04  来源:igfitidea点击:

Declaring an array of objects in a Spring bean context

javaspringjavabeans

提问by Alex Ciminian

I'm trying to create an array of objects in a Spring context file so I can inject it to a constructor that's declared like this:

我正在尝试在 Spring 上下文文件中创建一个对象数组,以便我可以将它注入到这样声明的构造函数中:

public RandomGeocodingService(GeocodingService... services) { }

I'm trying to use the <array>tag:

我正在尝试使用<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>

I haven't been able to find an example or something in the in the documentation on how to do this. Also, you have any suggestions for a better way of acheiving what I'm trying to do, please let me know :).

我无法在文档中找到有关如何执行此操作的示例或内容。此外,您对更好地实现我正在尝试做的事情有任何建议,请告诉我:)。

采纳答案by skaffman

That's because there's no such thing as <array>, there's only <list>.

那是因为没有这样的东西<array>,只有<list>.

The good news is that Spring will auto-convert between lists and arrays as required, so defined your array as a <list>, and Spring will be coerce it into an array for you.

好消息是 Spring 将根据需要在列表和数组之间自动转换,因此将您的数组定义为 a <list>,Spring 会为您将其强制转换为数组。

This should work:

这应该有效:

<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 will also coerce a single bean into a list, if required:

如果需要,Spring 还会将单个 bean 强制转换为列表:

<bean id="geocodingService" class="geocoding.RandomGeocodingService">
    <constructor-arg>
       <ref bean="googleGeocodingService"/>
    </constructor-arg>
</bean>

回答by earldouglas

Check out the util schema.

查看util 架构

回答by wandi.darko

Spring can automatically convert a list into an array[]

Spring 可以自动将列表转换为数组[]

check it out http://forum.springsource.org/showthread.php?37767-Injecting-String-Array

看看http://forum.springsource.org/showthread.php?37767-Injecting-String-Array

<bean name="test" class="Test">
   <property name="values" value="hugo,emil"></property>
</bean>

回答by Sandoval0992

I would like to know why the user who gave the best answer says ...

我想知道为什么给出最佳答案的用户说...

"That's because there's no such thing as <array>, there's only <list>"

“那是因为没有<array>,只有<list>

I'm currently using <array>tag to inject an array of objects into a bean.

我目前正在使用<array>标记将一组对象注入到 bean 中。

Take a look at the following code...

看看下面的代码...

    <bean id="song1" class="mx.com.company.songs.Song">
        <property name="name" value="Have you ever seen the rain?"/>        
    </bean>

    <bean id="song2" class="mx.com.company.songs.Song">
        <property name="name" value="La bamba"/>      
    </bean>

    <bean id="guitarPlayer" class="mx.com.company.musician.GuitarPlayer">
        <property name="songs">
            <array>
                <ref bean="song1"/>
                <ref bean="song2"/>
            </array>
        </property>
    </bean>