在春天注入arraylist时发出问题

时间:2013-10-09 14:00:15

标签: java spring

我使用的是弹簧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());


    }

}

但是当我尝试加载弹簧配置文件时,我得到以下异常。

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

我不明白为什么春天会给出这样的例外?我是否错过了弹簧配置中的某些内容?

2 个答案:

答案 0 :(得分:1)

删除<list>代码:

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

    <util:list list-class="java.util.ArrayList" value-type="com.springaction.testmultidimesionalcollection.Country">
            <ref bean="countryChina"/>
            <ref bean="countryIndia"/>
            <ref bean="countryAus"/>
    </util:list>

答案 1 :(得分:-1)

不是这个

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

但是这个

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