我在application-context.xml中有以下简单表达式:
<bean id="instrument" class="com.ustunozgur.Instrument" init-method="initialize" scope="prototype">
<property name="age" value="#{4}"/>
<property name="name" value="Violin"/>
Instrument类是一个简单的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中的初始bean声明:
<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.xml中包含了spring-core,spring-expression,spring-context。我没有通过代码进行任何配置;所有配置都是通过xml完成的。
PS:这是一个命令行应用程序,它可能是罪魁祸首吗?
PPS:以下代码有效,因此似乎只忽略XML中的spel:
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'");
String message = (String) exp.getValue();
这是我的完整application-context.xml和pom.xml:http://paste.pocoo.org/show/494260/和http://paste.pocoo.org/show/494262/
答案 0 :(得分:6)
确保使用ApplicationContext
而不是BeanFactory
。 BeanFactory
不支持ApplicationContext
的某些高级功能,包括Spring EL。
另见:
答案 1 :(得分:1)
对于简单的数字属性,您不需要表达式语言。字符串转换的数字由默认属性编辑器处理。
<property name="age" value="4"/>