【问题标题】:How to do string concatination in nested SpEL expression如何在嵌套 SpEL 表达式中进行字符串连接
【发布时间】:2021-03-17 23:22:58
【问题描述】:

我正在尝试为嵌套 SpEL 表达式执行 stringConcat -

我从一个 bean 获取主机名

<bean id="localhost" class="java.net.InetAddress" factory-method="getLocalHost" />

现在在另一个 bean 中,我想根据一些正则表达式匹配在我的构造函数的输入中附加 hostName。

<bean id="worker" class="a.b">
    <constructor-arg value="#{localhost.hostName matches '.*north-usa.*' ? '${taskList}-#{conf.version}-#{localhost.hostName}' : '${taskList}-#{conf.version}' "/>
</bean>

出于某种原因,这是 this 的输出,这是仅解析 taskList 的文字字符串 -

taskListA-#{conf.version}-#{localhost.hostName}

我想实现

taskListA-someConfVersion-someHostName

到目前为止我已经尝试过

<bean id="worker" class="a.b">
    <constructor-arg value="#{localhost.hostName matches '.*north-usa.*' ? "${taskList}-#{conf.version}-#{localhost.hostName}" : "${taskList}-#{conf.version}" "/>
</bean>

<bean id="worker" class="a.b">
    <constructor-arg value="#{localhost.hostName matches '.*north-usa.*' ? '${taskList}-conf.version-localhost.hostName' : '${taskList}-#conf.version' "/>
</bean>

<bean id="worker" class="a.b">
    <constructor-arg value="#{localhost.hostName matches '.*north-usa.*' ? '#(${taskList}-conf.version-localhost.hostName)' : '${taskList}-conf.version' "/>
</bean>

但他们都没有工作。任何帮助将不胜感激。

【问题讨论】:

    标签: spring spring-el


    【解决方案1】:

    我不知道SpEL中是否有类似嵌套表达式的东西,但我知道如何进行字符串连接:

    #{localhost.hostName matches '.*north-usa.*' ? '${taskList}-' + conf.version + '-' + localhost.hostName : '${taskList}-' + conf.version}
    

    【讨论】:

      猜你喜欢
      • 2014-07-30
      • 1970-01-01
      • 2015-06-29
      • 2012-05-19
      • 2012-05-28
      • 2015-07-10
      • 2012-10-07
      相关资源
      最近更新 更多