gpt4 book ai didi

java - 从字符串到自定义类的 ConversionNotSupportedException

转载 作者:行者123 更新时间:2023-12-01 22:35:34 24 4
gpt4 key购买 nike

我的配置文件有以下 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-2.5.xsd">
<bean id="testBean" class="com.example.MyClass">
<property name="client" value="com.example.otherclass.Other"></property>
</bean>

我的类(class) MyClass 是

public class MyClass implements MyInterface {
Other client;


@Override
public void doIt() {
// TODO Auto-generated method stub
try {
System.out.println(client.getInfo());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public Other getClient() {
return client;
}


public void setClient(Other client) {
this.client = client;
}


}

为什么我会得到

无法将属性“client”的类型 [java.lang.String] 的值转换为所需类型 [com.example.otherclasses.Other]:找不到匹配的编辑器或转换策略

最佳答案

您正在将 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>

关于java - 从字符串到自定义类的 ConversionNotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26928186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com