繁体   English   中英

在Spring中使用表达语言时出错

[英]Error in using Expression Language in Spring

无法将[java.lang.String]类型的属性值转换为属性“ item”的必需类型[com.spring.first.Item]; 嵌套的异常为java.lang.IllegalArgumentException:无法将属性“项目”的[java.lang.String]类型的值转换为所需的[com.spring.first.Item]类型:没有找到匹配的编辑器或转换策略

server.java

package com.spring.first;
public class Server {
    private Item item;
    private String itemName;
    public Item getItem()
    {
        return item; 
    }

    public String getItemName()
    {
        return itemName;
    }
    public void setItem(Item item)
    {
        this.item=item;
    }
    public void setItemName(String str)
    {
        this.itemName=str;
    }
    @Override
    public String toString()
    {
        return "Server [item ="+item+", itemName ="+itemName+"]";
    }
}

Item.java

public class Item {
    private String name;
    private int qty;
    public String getName()
    {
        return name;
    }
    public int getQty()
    {
        return qty;
    }
    public void setName(String name)
    {
        this.name=name;
    }
    public void setQty(int x)
    {
        this.qty=x;
    }
    @Override
    public String toString()
    {
        return "Item [ name ="+name+", Qty ="+qty+"];";
    }

}

我的配置文件

<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-3.0.xsd">

<bean id="itemBean" class="com.spring.first.Item">
        <property name="name" value="itemA" />
        <property name="qty" value="10" />
    </bean>
    <bean id="serverBean" class="com.spring.first.Server">
        <property name="item" value="#{itemBean}" />
        <property name="itemName" value="#{itemBean.name}" />
    </bean>

</beans>

我正在使用Spring 2.5.6。

Spring Exression Language(SPEL)是在Spring 3中引入的(请参阅Spring 3中引入的新功能 ),因此无法在Spring 2.5.6中使用SPEL。

您将需要将您的Spring版本至少升级到3(最好是最新版本,当前是4.2.2)。 您所拥有的配置是正确的,并且可以正常工作( 请参阅SPEL参考 )。

尝试如下更改配置文件

<bean id="itemBean" class="com.spring.first.Item">
    <property name="name" value="itemA" />
    <property name="qty" value="10" />
</bean>
<bean id="serverBean" class="com.spring.first.Server">
    <property name="item" ref="itemBean" />
    <property name="itemName" value="itemBean.name" />
</bean>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM