Declaring an array of objects in a Spring bean context

无人久伴 提交于 2019-11-27 17:21:47

问题


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:

public RandomGeocodingService(GeocodingService... services) { }

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

<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 :).


回答1:


That's because there's no such thing as <array>, there's only <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.

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:

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



回答2:


Spring can automatically convert a list into an array[]

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

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



回答3:


Check out the util schema.




回答4:


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>"

I'm currently using <array> tag to inject an array of objects into a 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> 


来源:https://stackoverflow.com/questions/3610804/declaring-an-array-of-objects-in-a-spring-bean-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!