Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring IDREF usage

Tags:

spring

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.

like image 473
yapkm01 Avatar asked Jan 30 '13 15:01

yapkm01


People also ask

How to use idref in Spring?

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.

What is the use of REF tag in XML based configuration metadata?

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.

Why we use XML file in spring?

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.

How do you define a spring bean?

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.


1 Answers

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.

like image 179
Michał Politowski Avatar answered Sep 20 '22 18:09

Michał Politowski