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>
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
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.
In spring, idref is used to pass the id or name of a bean as a string to other bean. Before using the idref, ensure that the bean must be defined in configuration with that id or name, otherwise spring will throw exception. The value pass by the idref attribute is always a string.
And ref is the element used to pass a bean that it is referring to. In other words it is used to pass reference of a bean to another bean (a collaborator) managed by spring container. The ref element is the final element inside a <property/> or <constructor-arg/> element.
There are advantages to using XML (or some other similar text-based approach though). In particular, you can change your application's wiring without rebuilding anything. If you don't want that, you can certainly do it all by hand or use something like Guice.
A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container.
idref
is used to pass the name (identifier) of a bean (that is, a String).
<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.
See the Spring documentation for details.
To pass the actual bean just use ref
, exactly as you do for pointB
and pointC
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With