简体   繁体   English

从messages.properties访问application.properties

[英]Access application.properties from messages.properties

I need to access properties from application.properties in messages.properties: 我需要从messages.properties中的application.properties访问属性:

application.properties application.properties

max-size=3

messages.properties messages.properties

alert.error=You can only save {max-size} items.

Not working: 无法运作:

{max-size}, #{max-size}, ${max-size}

I am aware of this thread, but I need it outside of any Java file. 我知道这个线程,但是我需要任何Java文件之外的线程。

Update: the maven approach works within application.properties files but not between application.properties and messages.properties. 更新: maven方法在application.properties文件中有效,但在application.properties和messages.properties之间无效。 Shouldn't there be an order? 不应该下订单吗? If file A has key a but when file B is parsed first a is not available in B yet, is it? 如果文件A具有密钥a,但是首先解析文件B时,a在B中尚不可用,是吗?

Snippet from my pom.xml: 我的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')})}" “#{alert.error($ {@ environment.getProperty( '最大规模')})}”

Either you replace it using build tools (Maven/Gradle/...), or you use a parameter in your message, like this: 您可以使用构建工具(Maven / Gradle / ...)替换它,也可以在消息中使用参数,如下所示:

alert.error=You can only save {0} items.

Now you can autowire your MessageSource and retrieve your maxSize : 现在,您可以自动连接MessageSource并检索您的maxSize

@Autowired
private MessageSource messageSource;
@Value("${max-size}")
private int maxSize;

And then you can use it this way: 然后您可以通过以下方式使用它:

messageSource.getMessage("alert.error", new Object[]{maxSize}, locale);

This solution allows you to use your message for other sizes as well, rather than making it just 3. 通过此解决方案,您还可以将消息用于其他大小,而不仅仅是将其设置为3。

If you want to use it in your views (eg. with Thymeleaf), you can do: 如果您想在视图中使用它(例如,与Thymeleaf一起使用),则可以执行以下操作:

<p th:text="#{alert.error(${@environment.getProperty('max-size')})}"></p>

If you're building in maven you can filter the message.properties at build time. 如果要在Maven中进行构建,则可以在构建时过滤message.properties。

https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

you can do via maven by invoking a resource plugin goal https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html 您可以通过调用资源插件目标https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html通过maven进行操作

where messages.properties is placed in src/main/resources 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>

invoke mvn resources:resources 调用mvn resources:resources

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM