How to use the properties file outside the WEB-INF directory?

I have a Spring MVC project (created using maven), and I spent the whole day looking for a way to use the properties file outside the directory WEB-INF.

For example, I tried putting it in a resource directory, but this does not work at all.

If I put the properties file in a directory src/main/resources, then compiling the project places this file in the directory target/{project name}/WEB-INF/classesso that the org.springframework.context.support.ReloadableResourceBundleMessageSourcebean cannot read it.

If I put the properties file in a directory WEB-INF, then the file will be copied to the directory target/{project name}/WEB-INFand the ReloadableResourceBundleMessageSourcebean will be able to read it.

Is it possible to use a properties file when I put it outside the directory WEB-INF, for example, inside the resource directory?

+4
source share
1 answer

You can install basenamesat ReloadableResourceBundleMessageSource. From the #setBasenamesdocumentation:

Define an array of base names, each of which corresponds to the basic ResourceBundle convention, not specifying the file extension or language codes, but unlike the ResourceBundleMessageSource, which refers to the location of the Spring resource: for example. "WEB-INF / messages" for "WEB-INF / messages.properties", "WEB-INF / messages_en.properties" etc.

XML property files are also supported: .g. "WEB-INF / messages" will find and download "WEB-INF / messages.xml", "WEB-INF / messages_en.xml, etc.

. , - .

XML, :

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
        p:basenames="classpath:localization/messages" />

java config :

@Bean
public ReloadableResourceBundleMessageSource messageSource() {
    final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    // other configurations
    messageSource.setBasenames("classpath:localization/messages");
    return messageSource;
}

properties classpath, messages*.* localization. java src\main\resources\localization.

+3

Source: https://habr.com/ru/post/1583869/


All Articles