在 Spring 中使用表达式语言时出错
Error in using Expression Language in Spring
Failed to convert property value of type [java.lang.String] to required type [com.spring.first.Item] for property 'item'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.spring.first.Item] for property 'item': no matching editors or conversion strategy found
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 Expression Language (SPEL) 是在Spring 3 中引入的(参考new features introduced in Spring 3),因此无法在Spring 2.5 中使用SPEL .6.
您需要将 Spring 版本至少升级到 3(最好是最新版本,目前为 4.2.2)。您拥有的配置是正确的并且可以工作(see SPEL reference)。
尝试如下更改配置文件
<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>
Failed to convert property value of type [java.lang.String] to required type [com.spring.first.Item] for property 'item'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.spring.first.Item] for property 'item': no matching editors or conversion strategy found
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 Expression Language (SPEL) 是在Spring 3 中引入的(参考new features introduced in Spring 3),因此无法在Spring 2.5 中使用SPEL .6.
您需要将 Spring 版本至少升级到 3(最好是最新版本,目前为 4.2.2)。您拥有的配置是正确的并且可以工作(see SPEL reference)。
尝试如下更改配置文件
<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>