简体   繁体   English

如何让SpEL将两个数字相加

[英]How to get SpEL to add two numbers

I want to compute my port number inside application.properties like this: 我想像这样在application.properties中计算我的端口号:

server.port=#{ 1 + ${myapp.web.server.port.ssl} }
myapp.web.server.port.ssl=8300

But when I start my Spring Boot app all I get is an execption. 但是,当我启动Spring Boot应用程序时,我得到的只是执行。

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.lang.String to type java.lang.Integer]

The Mathematical Operators section of the reference guide suggests that it should work, but it doesn't. 参考指南的 “数学运算符”部分建议它应该起作用,但不能起作用。

Inside Spring it gets as far as calling: 在Spring内部,它可以调用以下代码:

GenericConversionService.convert("#{ 1 + 8300 }", java.lang.String, java.lang.Integer)

But there is no logic in there for recognising it's SpEL. 但是,那里没有逻辑可以识别出SpEL。 It's like it's not recognising it's SpEL at all. 就像根本没有意识到它是SpEL一样。 However if I change the expression to #{ n1 + ${myapp.web.server.port.ssl} } then I get the following exception, showing that it is being evaluated as SpEL: 但是,如果我将表达式更改为#{ n1 + ${myapp.web.server.port.ssl} }则会收到以下异常,表明该表达式被评估为SpEL:

SpelEvaluationException: EL1008E:(pos 0): Property or field 'n1' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?

How can I make SpEL add one to the value of another property? 如何使SpEL在另一个属性的值上加上一个?

Update: 更新:

I found a similar question so I tried a similar approach: 我发现了类似的问题,所以尝试了类似的方法:

server.port=#{T(java.lang.Integer).parseInt('${fire.web.server.port.ssl}') + 1} server.port =#{T(java.lang.Integer).parseInt('$ {fire.web.server.port.ssl}')+ 1}

But that still doesn't work. 但这仍然行不通。 For some reason it's not treating it as an SpEL expression. 由于某种原因,它没有将其视为SpEL表达式。 But is does treat it as a SpEL expression if there is a syntax error! 但是如果存在语法错误,确实会将其视为SpEL表达式!

You can do that only from config, but not from properties: 您只能通过config来执行此操作,而不能通过属性来执行此操作:

<bean class="...">
   <property name="port" value="#{1 + ${myapp.web.server.port.ssl}}"/>
</bean>

I think the problem is that ${myapp.web.server.port.ssl} is evaluated as String , try something like this 我认为问题在于${myapp.web.server.port.ssl}被评估为String ,尝试这样的事情

#{1 + new Integer(props['myapp.web.server.port.ssl'])}

where props is the bean holding your properties. 其中props是保存您的属性的bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM