Spring IDREF的用法 [英] Spring IDREF usage
问题描述
我有如下定义的spring.xml:
I have a spring.xml defined as per below:
<?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="org.tutorial.spring.Triangle">
<property name="pointA">
<idref bean="pointA"/>
</property>
<property name="pointB" ref="pointB"/>
<property name="pointC" ref="pointC"/>
</bean>
<bean id="pointA" class="org.tutorial.spring.Point">
<property name="x" value="0"/>
<property name="y" value="0"/>
</bean>
<bean id="pointB" class="org.tutorial.spring.Point">
<property name="x" value="100"/>
<property name="y" value="200"/>
</bean>
<bean id="pointC" class="org.tutorial.spring.Point">
<property name="x" value="-100"/>
<property name="y" value="-200"/>
</bean>
</beans>
Point
类基本上是具有2个私有int成员的类.我的问题是我在IDREF上收到如下错误:
The Point
class is basically a class with 2 private int members. My problem is i'm getting the error on IDREF as per below:
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.tutorial.spring.Point' for property 'pointA';
nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.tutorial.spring.Point] for property 'pointA': no matching editors or conversion strategy found
据我所知,IDREF的目的(在上述情况下)是存在bean PointA
(错误检查)的bean三角形.所以我确实在IDREF元素中提供了bean PointA
(string)的名称.为什么会出现上述错误?
当我认为仅通过提供其名称来检查bean(PointA
)是否存在时,为什么尝试将字符串转换为Point?
As far as i understand, the purpose of the IDREF (in the above case) that bean PointA
exists (error check) for bean triangle. So i did supply the name of bean PointA
(string) in the IDREF element. Why do i get the above error?
Why is it trying to convert a string to Point when i thought it is just checking the existence of a bean (PointA
) by just supplying its name?
我真的很困惑.请帮忙.谢谢.
I'm really confused. Please help. Thanks.
推荐答案
idref
用于传递bean(即字符串)的名称(标识符).
idref
is used to pass the name (identifier) of a bean (that is, a String).
<idref bean="pointA">
与字符串值pointA
完全相同,除了如果未定义这样的bean,Spring会抱怨.
<idref bean="pointA">
is exactly the same as just the string value pointA
, except that Spring will complain if such a bean is not defined.
请参见 Spring文档了解详情.
要传递实际的bean,只需使用ref
,就像对pointB
和pointC
所做的一样.
To pass the actual bean just use ref
, exactly as you do for pointB
and pointC
.
这篇关于Spring IDREF的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!