【问题标题】:Error while using idref in Spring在 Spring 中使用 idref 时出错
【发布时间】:2014-12-25 16:24:37
【问题描述】:

我收到以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'triangle' defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'com.mycompany.springdemo.Point' for property 'point0'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.mycompany.springdemo.Point] for property 'point0': no matching editors or conversion strategy found

我的spring配置文件是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-      beans-2.0.dtd">

<beans>
    <bean id="triangle" class="com.mycompany.springdemo.Triangle">
        <property name="point0">
            <idref bean = "point01"/>
        </property>
        <property name="point1">
            <bean class="com.mycompany.springdemo.Point">
                <property name="x" value="20"/>
                <property name="y" value="0"/>
            </bean>
        </property>
        <property name="point2">
            <bean class="com.mycompany.springdemo.Point">
                <property name="x" value="0"/>
                <property name="y" value="20"/>
           </bean>
        </property>
    </bean>
    <bean id="point01" class="com.mycompany.springdemo.Point">
        <property name="x" value="0"/>
        <property name="y" value="0"/>
    </bean>  

</beans>

如果我使用 'ref' 来初始化 point0,那么它可以正常工作,但是对于 'idref' 会出错。

【问题讨论】:

标签: java spring dependency-injection


【解决方案1】:

代替:

<property name="point0">
            <idref bean = "point01"/>
</property>

使用这个:

<property name="point0" ref="point01" />

and read the documentation about idref

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 2021-10-13
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    相关资源
    最近更新 更多