我试图在int:header中格式化时间戳并遇到与SpEL相关的奇怪行为(事实上我已经发现,header-richher应该只用于静态值,所以我的方法是错误的首先,但我仍然对我所遇到的例外情况感到好奇。
<int:header-enricher>
<int:header name="originalFilename" expression="#{T(org.apache.commons.lang3.time.DateFormatUtils).format(new java.util.Date(), 'yyyyMMddhhmm')}"/>
</int:header-enricher>
堆栈追踪:
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.integration.transformer.support.ExpressionEvaluatingHeaderValueMessageProcessor]: Constructor threw exception; nested exception is org.springframework.expression.spel.SpelParseException: EL1035E:(pos 26): The value '201504101118' cannot be parsed as an int
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:164) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:125) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:270) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE]
... 71 common frames omitted
Caused by: org.springframework.expression.spel.SpelParseException: EL1035E:(pos 26): The value '201504101118' cannot be parsed as an int
at org.springframework.expression.spel.ast.Literal.getIntLiteral(Literal.java:79) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.maybeEatLiteral(InternalSpelExpressionParser.java:736) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatStartNode(InternalSpelExpressionParser.java:459) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatPrimaryExpression(InternalSpelExpressionParser.java:301) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatUnaryExpression(InternalSpelExpressionParser.java:295) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatPowerIncDecExpression(InternalSpelExpressionParser.java:251) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatProductExpression(InternalSpelExpressionParser.java:230) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatSumExpression(InternalSpelExpressionParser.java:216) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatRelationalExpression(InternalSpelExpressionParser.java:168) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatLogicalAndExpression(InternalSpelExpressionParser.java:156) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatLogicalOrExpression(InternalSpelExpressionParser.java:144) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatExpression(InternalSpelExpressionParser.java:104) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:85) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:56) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:32) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:76) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:62) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
at org.springframework.integration.transformer.support.ExpressionEvaluatingHeaderValueMessageProcessor.<init>(ExpressionEvaluatingHeaderValueMessageProcessor.java:62) ~[spring-integration-core-4.0.4.RELEASE.jar:na]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_75]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.7.0_75]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.7.0_75]
at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:1.7.0_75]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148) ~[spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE]
... 73 common frames omitted
Caused by: java.lang.NumberFormatException: For input string: "201504101118"
at java.lang.NumberFormatException.forInputString(Unknown Source) ~[na:1.7.0_75]
at java.lang.Integer.parseInt(Unknown Source) ~[na:1.7.0_75]
at org.springframework.expression.spel.ast.Literal.getIntLiteral(Literal.java:75) ~[spring-expression-4.0.7.RELEASE.jar:4.0.7.RELEASE]
说真的,为什么要java.lang.Integer.parseInt?
当我尝试时会发生同样的事情:
#{T(org.apache.commons.lang3.time.DateFormatUtils).format(1428647461944L, 'yyyyMMddhhmm')}"/>
我还发现了这个开放的jira问题:https://jira.spring.io/browse/SPR-11021
这是异常的原因吗?
答案 0 :(得分:1)
@rompetroll的评论是正确的; "#{...}"
语法用于在上下文初始化时评估一次的静态值。
Spring集成使用运行时表达式,每次消息到达组件时都会对其进行评估。只需删除#{
和}
。
您正尝试将静态值注入需要表达式的属性。