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

标签 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-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/

相关文章:

java - 更改属性文件的名称导致找不到它

java - Spring Data @Query 使用 VARCHAR 而不是 NVARCHAR

java - Java Spring中的重载 Controller 方法

Spring MVC : Making multi user friendly

java - 使用自己的 HashSet.add() 实现

java - 如何以编程方式添加 ProgressBar?

Java 匿名迭代器?

java - OnClickListener 单击按钮后直接更改页面

java - 如何在测试中@Autowired BuildProperties?

java - 如何使用 Spring 在 Postgresql 中存储 java.util.ArrayList