我需要访问application.properties在messages.properties中的属性:
application.properties
max-size=3messages.properties
alert.error=You can only save {max-size} items.不起作用:
{max-size}, #{max-size}, ${max-size}我知道this线程,但我需要它在任何Java之外。
更新:方法在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')})}“
发布于 2016-10-18 13:45:02
要么使用构建工具(Maven/Gradle/.)替换它,要么在消息中使用参数,如下所示:
alert.error=You can only save {0} items.现在您可以自动生成MessageSource并检索您的maxSize
@Autowired
private MessageSource messageSource;
@Value("${max-size}")
private int maxSize;然后你就可以这样使用它:
messageSource.getMessage("alert.error", new Object[]{maxSize}, locale);此解决方案允许您将您的消息用于其他大小,而不是只使用3。
如果你想在你的观点中使用它(如。(使用Thymeleaf),您可以:
<p th:text="#{alert.error(${@environment.getProperty('max-size')})}"></p>发布于 2016-10-18 12:58:17
如果您正在构建maven,您可以在构建时过滤message.properties。
https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
发布于 2016-10-18 13:15:25
您可以通过maven调用资源插件目标https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html来实现。
其中messages.properties放在src/main/resources中。
<build>
<filters>
<filter>src/main/resources/application.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>调用mvn resources:resources
https://stackoverflow.com/questions/40108713
复制相似问题