I'm learning Spring only using the documentation. I got confused when idref came into picture. My understanding is in order to define a property in a bean we need to use the "property" tag with "name" and "value" attributes. "name" should be exactly the name we give for the property in the POJO and "value" can be any primitive datatype. If we need to map it to a separate bean then we should use the "ref" attribute instead. This "ref" attribute must hold the "id" of the bean we refer to.
Now as per the documentation we have a snippet that shows "value" attribute referring to another bean. The documentation recommends the use of "idref" instead. When I tried this real time , I get the following exception.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'theClientBean' defined in class path resource [ConstructorInjectionDemo.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.javagrasp.dependencies.TheTargetBean' for property 'targetName'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.javagrasp.dependencies.TheTargetBean] for property 'targetName': no matching editors or conversion strategy found
Bean Definition:
<bean id="theTargetBean" class="com.javagrasp.dependencies.TheTargetBean"/>
<bean id="theClientBean" class="com.javagrasp.dependencies.TheClientBean">
<property name="targetName">
<idref bean="theTargetBean" />
</property>
</bean>
Class details:
ClientBean class:
public class TheClientBean {
private TheTargetBean targetName;
public void setTargetName(TheTargetBean targetName) {
this.targetName = targetName;
}
}
TargetBean Class:
package com.javagrasp.dependencies;
public class TheTargetBean {
}
Also:
What does the below line means , do I need to name the property always in some convention ?
17:44:22.270 [main] DEBUG org.springframework.beans.BeanUtils - No property editor [com.javagrasp.dependencies.TheTargetBeanEditor] found for type com.javagrasp.dependencies.TheTargetBean according to 'Editor' suffix convention