Search code examples
javaspringexceptionspring-el

Simple Spring EL expression does not work; with error TypeMismatchException


I have the following simple expression in my 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"/>

The Instrument class is a simple POJO. However, it is throwing the following error:

[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}" -> 

Here is the initial beans declaration in my xml:

<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">

What could be the problem? I have included spring-core, spring-expression, spring-context in my pom.xml. I am not doing any configuration via code; all the configuration is done via xml.

PS: This is a command line application, can it be the culprit?

PPS: The following code works though, so it seems only the spel in XML is ignored:

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

Here is my complete application-context.xml and pom.xml: http://paste.pocoo.org/show/494260/ and http://paste.pocoo.org/show/494262/


Solution

  • Make sure you use ApplicationContext rather than BeanFactory. BeanFactory doesn't support some advanced features of ApplicationContext, including Spring EL.

    See also: