在Spring中的idref和ref的区别

时间:2015-04-22 04:14:30

标签: spring spring-4

我只使用文档学习Spring。当idref进入画面时我感到困惑。我的理解是为了在bean中定义一个属性,我们需要使用"属性"标记为"名称"和"价值"属性。 "名称"应该是我们为POJO中的财产提供的名称和"值"可以是任何原始数据类型。如果我们需要将它映射到一个单独的bean,那么我们应该使用" ref"属性改为。这" ref"属性必须包含" id"我们所指的豆子。

现在根据文档我们有一个片段,显示"值"属性引用另一个bean。文档建议使用" idref"代替。当我实时尝试时,我得到以下异常。

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定义:

<bean id="theTargetBean" class="com.javagrasp.dependencies.TheTargetBean"/>

<bean id="theClientBean" class="com.javagrasp.dependencies.TheClientBean">
<property name="targetName">
    <idref bean="theTargetBean" />
</property>
</bean>

课程详情:

ClientBean类:

public class TheClientBean  {

private TheTargetBean targetName;

public void setTargetName(TheTargetBean targetName) {
    this.targetName = targetName;
}
}

TargetBean类:

package com.javagrasp.dependencies;
public class TheTargetBean {

}

此外:

下面的内容是什么意思,我是否需要在某些惯例中命名属性?

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

1 个答案:

答案 0 :(得分:0)

由于IdRef传递了你的bean应该喜欢的bean的名称

public class TheClientBean  {

    private String targetName;

   //Name of the Bean is passed here.
    public void setTargetName(String targetName) {
      this.targetName = targetName;
   }
}