【发布时间】:2015-06-29 12:37:13
【问题描述】:
我只使用文档来学习 Spring。当 idref 出现时,我感到很困惑。我的理解是为了在 bean 中定义一个属性,我们需要使用带有“name”和“value”属性的“property”标签。 “name”应该是我们为 POJO 中的属性指定的名称,“value”可以是任何原始数据类型。如果我们需要将它映射到一个单独的 bean,那么我们应该使用“ref”属性。这个“ref”属性必须持有我们引用的bean的“id”。
现在根据文档,我们有一个 sn-p,它显示引用另一个 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 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
【问题讨论】: