首页 > 解决方案 > Spring Boot - 不要解析 application.properties 文件中的占位符

问题描述

我正在使用带有 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("!", "");
}

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

标签: javaspringspring-bootapache-camelspring-camel

解决方案


你可以用

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

推荐阅读