【问题标题】:Spring Boot - Do not resolve placeholders in the application.properties file [duplicate]Spring Boot - 不要解析 application.properties 文件中的占位符[重复]
【发布时间】:2021-08-06 14:12:16
【问题描述】:

我正在使用带有 Camel 框架的 Spring Boot。

我需要保存一个属性,例如:

output.file=file://C:/Users/bfrisco/Desktop/output?fileName=${headers.fileName}

我需要这个原始字符串。我不希望 Spring 尝试解决 ${headers.fileName},我想要的正是我输入的文本。启动我的应用程序时,我会收到此错误:

无法解析占位符“headers.fileName”

这个值是动态的,会在运行时改变。 Camel 将以不同于 Spring 的方式解释这个属性。我一直无法找到一种方法来阻止 spring 在启动时尝试解释这一点。

我有这个可行的解决方法,但它并不理想,我觉得有更好的方法:

# Inserted a ! between so Spring does not interpret it
output.file=file://C:/Users/bfrisco/Desktop/output?fileName=$!{headers.fileName}

@Value("${output.file}")
private String outputFile;

@PostConstruct
public void init() {
    outputFile = outputFile.replaceAll("!", "");
}

有没有办法解决这个问题,而无需在每个需要使用这些值的类中实现此解决方法?

【问题讨论】:

    标签: java spring spring-boot apache-camel spring-camel


    【解决方案1】:

    你可以用

    output.file=file://C:/Users/bfrisco/Desktop/output?fileName=#{'$'}{headers.fileName}
    

    【讨论】:

    • 我试过了,不幸收到同样的问题。
    • 很抱歉,这只是一个猜测。请检查我的更新答案
    • 有效!谢谢。
    猜你喜欢
    • 2020-04-01
    • 2018-02-20
    • 1970-01-01
    • 2018-06-17
    • 2016-12-25
    • 2016-01-20
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    相关资源
    最近更新 更多