有 Java 编程相关的问题?

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

java简单Spring EL表达式不起作用;带有错误类型失配异常

在我的应用程序上下文中有以下简单表达式。xml:

<bean id="instrument" class="com.ustunozgur.Instrument" init-method="initialize" scope="prototype">
<property name="age" value="#{4}"/>
<property name="name" value="Violin"/>

乐器类是一个简单的POJO。但是,它会抛出以下错误:

[ERROR] ...nested exception is org.springframework.beans.TypeMismatchException: 
Failed     to convert property value of type 'java.lang.String' to required type
'int'   for      property 'age'; nested exception is java.lang.NumberFormatException: For input string:
"{4}" -> 

以下是我的xml中的初始beans声明:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

有什么问题吗?我的pom中包括了spring核心、spring表达式和spring上下文。xml。我没有通过代码进行任何配置;所有配置都是通过xml完成的

PS:这是一个命令行应用程序,它可能是罪魁祸首吗

PPS:但以下代码仍然有效,因此似乎只忽略了XML中的spel:

  ExpressionParser parser = new SpelExpressionParser();
  Expression exp = parser.parseExpression("'Hello World'");
  String message = (String) exp.getValue();

这是我完整的应用程序上下文。xml和pom。xml:http://paste.pocoo.org/show/494260/http://paste.pocoo.org/show/494262/


共 (2) 个答案

  1. # 1 楼答案

    对于简单的数值属性,不需要表达式语言。数字到字符串的转换由默认属性编辑器处理

    <property name="age" value="4"/>