2

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の最初の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.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/

4

2 に答える 2

6

ApplicationContextではなく必ず使用してくださいBeanFactory。Spring EL を含むBeanFactoryの一部の高度な機能をサポートしていません。ApplicationContext

以下も参照してください。

于 2011-10-18T09:17:33.983 に答える
1

単純な数値プロパティの場合、式言語は必要ありません。数値から文字列への変換は、既定のプロパティ エディターによって処理されます。

<property name="age" value="4"/>
于 2011-10-18T09:12:41.310 に答える