【问题标题】:Add dynamic value in spel expression在 spel 表达式中添加动态值
【发布时间】:2019-07-04 00:50:35
【问题描述】:

我有一个常量值的字符串,我想在使用 MV 网关时将它传递给 rename_to 标头。

我尝试了下面的代码 sn-p 和一个通过在上下文中添加变量然后将其与 #basePath 一起使用的方法

@Value("${basePath:/home/}")
String basePath;

.enrichHeaders(h -> h
                        .headerExpression(RENAME_TO, "'${basePath}' + headers[file_remoteFile]")
                        .headerExpression(REMOTE_FILE, "headers[file_remoteFile]")
                        .header(REMOTE_DIRECTORY, "headers[file_remoteDirectory]"))

我在启动时遇到错误。如何在我的 application.properties 中提供 basePath

【问题讨论】:

    标签: spring-integration spring-el spring-integration-sftp


    【解决方案1】:

    @Value("${basePath:/home/}")

    String basePath;

    表示“如果没有basePath 属性,则将'/home/' 注入变量basePath

    您不能像在 SPeL 表达式中那样使用封闭类中的字段,也不能在 SpEL 中使用属性占位符;你必须在java中连接字符串。

    .headerExpression(RENAME_TO, "'" + this.basePath + "'" + " + headers[file_remoteFile]")
    

    【讨论】:

    • 简而言之,我想将 basePath 放在我的 application.properties 文件中,而不是放在类中。
    • 查看我的答案的编辑;我没有意识到您不能在这些表达式中使用属性占位符。您需要在 java 中进行连接。
    • Answer 有一个额外的报价,我已经删除了它并且它正在工作。甚至更新了答案。
    • @GaryRussell“你不能在 SpEL 中使用属性占位符”是否有启用此功能的错误/功能请求?我花了大约两个小时试图获得#{}${} 的正确组合以使其工作只是为了发现这是不可能的,我不得不求助于字符串连接。
    • 我不这么认为;随时在此处打开新功能请求:github.com/spring-projects/spring-integration/issues
    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多