gpt4 book ai didi

java - 简单的 Spring EL 表达式不起作用;出现错误 TypeMismatchException

转载 作者:行者123 更新时间:2023-12-01 15:21:45 25 4
gpt4 key购买 nike

我的 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 中的拼写被忽略了:

  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/

最佳答案

确保使用 ApplicationContext 而不是 BeanFactoryBeanFactory 不支持 ApplicationContext 的一些高级功能,包括 Spring EL。

另请参阅:

关于java - 简单的 Spring EL 表达式不起作用;出现错误 TypeMismatchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7804620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com