【发布时间】:2016-10-18 12:47:14
【问题描述】:
我需要从messages.properties中的application.properties访问属性:
application.properties
max-size=3
messages.properties
alert.error=You can only save {max-size} items.
不工作:
{max-size}, #{max-size}, ${max-size}
我知道 this 线程,但我需要它在任何 Java 文件之外。
更新: maven 方法适用于 application.properties 文件,但不适用于 application.properties 和 messages.properties。不应该有订单吗?如果文件 A 有密钥 a 但首先解析文件 B 时,a 在 B 中不可用,是吗?
来自我的 pom.xml 的片段:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
"#{alert.error(${@environment.getProperty('max-size')})}"
【问题讨论】:
-
在构建时从 application.properties 获取这些属性有什么价值? Spring 的最佳实践是使用 @value 为属性注入值。
-
@IssamELATIF 的语法是什么? alert.error=你只能保存???max-size???项目。
-
语法应该是 alert.error=您只能保存 {} 项,然后使用 MessageSource 获取消息。我删除了我的答案,因为它与@g00glen00b 的建议相同。
标签: java spring spring-boot internationalization