Java 从字符串到自定义类的转换NotSupportedException
我的配置文件有以下beanJava 从字符串到自定义类的转换NotSupportedException,java,spring,Java,Spring,我的配置文件有以下bean <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="testBean" class="com.example.MyClass">
<property name="client" value="com.example.otherclass.Other"></property>
</bean>
为什么我会受伤
无法将[java.lang.String]类型的值转换为属性“客户端”所需的[com.example.otherclasses.Other]类型:未找到匹配的编辑器或转换策略您正在将客户端的值设置为字符串
com.example.otherclass.Other
您需要执行以下操作:
<bean id="myOther" class="com.example.otherclass.Other">
</bean>
<bean id="testBean" class="com.example.MyClass">
<property name="client" ref="myOther"></property>
</bean>
您正在将client的值设置为字符串
com.example.otherclass.Other
您需要执行以下操作:
<bean id="myOther" class="com.example.otherclass.Other">
</bean>
<bean id="testBean" class="com.example.MyClass">
<property name="client" ref="myOther"></property>
</bean>
这个错误是可以自我解释的。您的setter需要一个
Other
对象,您将字符串“com.example.otherclass.Other”
传递给它。Spring有一些默认的转换器,可以将if转换为类
对象,但不能转换为其他
对象
如果您只想用一个新的Other
对象初始化client
属性,那么可以使用匿名(*)内部bean:
<bean id="testBean" class="com.example.MyClass">
<property name="client">
<bean class="com.example.otherclass.Other"/>
</property>
</bean>
(*)事实上,Spring会给bean起一个名字,但它被称为匿名,因为您通常不能按名字使用它(您不知道它的名字)。这个错误是非常自我解释的。您的setter需要一个
Other
对象,您将字符串“com.example.otherclass.Other”
传递给它。Spring有一些默认的转换器,可以将if转换为类
对象,但不能转换为其他
对象
如果您只想用一个新的Other
对象初始化client
属性,那么可以使用匿名(*)内部bean:
<bean id="testBean" class="com.example.MyClass">
<property name="client">
<bean class="com.example.otherclass.Other"/>
</property>
</bean>
(*)事实上,Spring会给bean起一个名称,但它被称为匿名,因为您通常不能按名称使用它(您不知道名称)