有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ConversionNotSupportedException从字符串到自定义类

我的配置文件有以下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>

我的班我的班是

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;
}


}

为什么我会

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


共 (1) 个答案

  1. # 1 楼答案

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