복붙노트

[SPRING] Spring 빈 컨텍스트에서 객체의 배열 선언하기

SPRING

Spring 빈 컨텍스트에서 객체의 배열 선언하기

나는 다음과 같이 선언 된 생성자에 삽입 할 수 있도록 Spring 컨텍스트 파일에 객체 배열을 만들려고합니다.

public RandomGeocodingService(GeocodingService... services) { }

태그를 사용하려고합니다.

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

나는 이것을 수행하는 방법에 대한 문서에서 예제 나 무언가를 찾을 수 없었다. 또한, 당신은 내가 뭘하려고하는지 더 잘 이해할 수있는 방법을 제안합니다. 제게 알려주십시오 :).

해결법

  1. ==============================

    1.이것은 와 같은 것이 없기 때문에 밖에 없습니다.

    이것은 와 같은 것이 없기 때문에 밖에 없습니다.

    좋은 소식은 Spring이 필요에 따라 목록과 배열을 자동 변환하기 때문에 배열을 로 정의하면 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 id="geocodingService" class="geocoding.RandomGeocodingService">
        <constructor-arg>
           <ref bean="googleGeocodingService"/>
        </constructor-arg>
    </bean>
    
  2. ==============================

    2.Spring은 자동적으로리스트를 배열로 변환 할 수있다.

    Spring은 자동적으로리스트를 배열로 변환 할 수있다.

    그것을 확인해 보라. http://forum.springsource.org/showthread.php?37767-Injecting-String-Array

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

    3.util 스키마를 확인하십시오.

    util 스키마를 확인하십시오.

  4. ==============================

    4.나는 왜 최고의 답변을 준 사용자가 말하는지 알고 싶다.

    나는 왜 최고의 답변을 준 사용자가 말하는지 알고 싶다.

    "와 같은 것이 없기 때문에 만 있습니다."

    현재 태그를 사용하여 객체 배열을 bean에 삽입합니다.

    다음 코드를 살펴보십시오 ...

        <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> 
    
  5. from https://stackoverflow.com/questions/3610804/declaring-an-array-of-objects-in-a-spring-bean-context by cc-by-sa and MIT license