0

私は春の 3.2.2 を使用しています。Spring で util:list タグを使用してコレクション (ArrayList) を注入しているときに問題に直面しています。以下の Bean 定義があります。

package com.springaction.testmultidimesionalcollection;


import java.util.List;

/**
 * @author jinesh
 *
 */
public class Address {


    List<Country> country;

    public List<Country> getCountry() {
        return country;
    }
    /**
     * @param country the country to set
     */

    public void setCountry(List<Country> country) {
        this.country = country;
    }


}

以下は、私が使用している春の構成ファイルです。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd"
   >
   <bean id="address" class="com.springaction.testmultidimesionalcollection.Address">
        <property name="country">
            <util:list list-class="java.util.ArrayList" value-type="com.springaction.testmultidimesionalcollection.Country">
                <list>
                    <ref bean="countryChina"/>
                    <ref bean="countryIndia"/>
                    <ref bean="countryAus"/>
                </list>
            </util:list>

        </property>
    </bean>
<bean id="countryChina" class="com.springaction.testmultidimesionalcollection.Country">
        <property name="countryName" value="China" />
    </bean>
    <bean id="countryIndia" class="com.springaction.testmultidimesionalcollection.Country">
        <property name="countryName" value="India" />
    </bean>
    <bean id="countryAus" class="com.springaction.testmultidimesionalcollection.Country">
        <property name="countryName" value="Australia" />
    </bean>
 </beans>

次のコードでこれをテストしようとしました。

package com.springaction.testmultidimesionalcollection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author jinesh
 *
 */

public class TestMultiDimensionalMain {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context =  new ClassPathXmlApplicationContext("com/springaction/testmultidimesionalcollection/testmultidimensionalcollection.xml");
        Address addrs=(Address)context.getBean("address");
        System.out.println("address size:" + addrs.getCountry().size());


    }

}

しかし、Spring 構成ファイルをロードしようとすると、以下の例外が発生します。

Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.ArrayList' to required type 'java.util.List' for property 'country'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.util.ArrayList] to required type [com.springaction.testmultidimesionalcollection.Country] for property 'country[0]': no matching editors or conversion strategy found
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:463)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:494)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:488)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1439)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1398)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1134)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    ... 11 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.util.ArrayList] to required type [com.springaction.testmultidimesionalcollection.Country] for property 'country[0]': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:264)
    at org.springframework.beans.TypeConverterDelegate.convertToTypedCollection(TypeConverterDelegate.java:559)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:199)
    at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:448)
    ... 17 more

春がそのような例外を与える理由がわかりませんか?春の設定で何かを見逃しましたか?

4

2 に答える 2

-1

Not this

<util:list list-class="java.util.ArrayList"

but this

<util:list list-class="java.util.List"
于 2015-03-02T14:20:17.130 に答える