1

I have two messages.properties files. One is located inside resources and another one is outside my .jar file in a directory called etc.

This is my PropertiesConfiguration class:

@Configuration
public class PropertiesConfiguration {

    @Bean
    public PropertyPlaceholderConfigurer properties() {
        final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        ppc.setIgnoreResourceNotFound(true);

        final List<Resource> resourceLst = new ArrayList<Resource>();

        resourceLst.add(new FileSystemResource("etc/application.properties"));
        resourceLst.add(new FileSystemResource("etc/messages.properties"));
        resourceLst.add(new FileSystemResource("etc/messages_et.properties"));

        ppc.setLocations(resourceLst.toArray(new Resource[]{}));

        return ppc;
    }
}

In the logs I see this:

11:18:43.764  INFO [main] PropertyPlaceholderConfigurer              - Loading properties file from file [C:\Users\deniss\IdeaProjects\repgen\etc\application.properties]
11:18:43.764  WARN [main] PropertyPlaceholderConfigurer              - Could not load properties from file [C:\Users\deniss\IdeaProjects\repgen\etc\application.properties]: etc\application.properties (The system cannot find the file specified)
11:18:43.764  INFO [main] PropertyPlaceholderConfigurer              - Loading properties file from file [C:\Users\deniss\IdeaProjects\repgen\etc\messages.properties]
11:18:43.764  INFO [main] PropertyPlaceholderConfigurer              - Loading properties file from file [C:\Users\deniss\IdeaProjects\repgen\etc\messages_et.properties]

As I understand my messages.properties from etc is loaded. Although when the application is working, the values from it are not used. They are coming from default messages.properties inside my resources project folder. Am I doing something wrong?

Deniss M.
  • 3,617
  • 17
  • 52
  • 100
  • 1
    You want a message source, but you are using property placeholder – sashok_bg Dec 01 '16 at 11:37
  • @sashok_bg you are correct. I have added another @Configuration bean to my project. `@Bean public ResourceBundleMessageSource messageSource() {` The problem is that I don't know how to point `messageSource.setBasename("");` to the files outside my .jar. `"classpath"` obviously doesn't work. – Deniss M. Dec 01 '16 at 11:50
  • According to spring docs, it is only possible to add a classpath, meaning inside the .jar Too bad. – Deniss M. Dec 01 '16 at 12:10
  • 1
    You can add anything to your classpath in theory – sashok_bg Dec 01 '16 at 13:36
  • @sashok_bg that is true. I found a solution by using setBaseName("file:etc/messages"). This now loads my messages.properties files from outside folder. – Deniss M. Dec 01 '16 at 13:42
  • **watch out!** assume you have file `path/to/messages_en.properties` on base name you have to set : `messageSource.setBasename("file:path/to/messages")` **omitting the suffix** `_en.properties` – Mike D3ViD Tyson Jul 25 '18 at 15:20

3 Answers3

3

my solution was:

@Configuration   
public class SpringConfiguration {

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("file:/path/to/file/messages");
        messageSource.setCacheSeconds(10); //reload every 10 sec..
        return messageSource;
    }
}

it works perfect on me, remember to omitt the suffix _et.properties on the base name, for example if you have a file named messages_et.properties set base name will result : messageSource.setBasename("file:/path/to/file/messages");

Mike D3ViD Tyson
  • 1,701
  • 1
  • 21
  • 31
  • 1
    How do I actually get a message from that default `messages.properties` (with no suffix in its name)? I am asking, because the `MessageSource`-object always requires a Locale and even if I give it `Locale#getDefault` the default properties-file is not queried. And of course there is no fallback unless I specify the default value as a string - which would render the default `messages.properties` file useless. – Igor Jul 19 '19 at 17:01
  • In my case it had to be like "file:\\C:\\users\\user\\messages\\labels", my files are named labels.properties and labels_es.properties. I am using windows 10, – carlos palma Aug 26 '20 at 22:12
0

I think there is an answer for this question here: Spring Boot and multiple external configuration files

That worked for me earlier, so it is worth to give it a try!

Community
  • 1
  • 1
Mr.Fireman
  • 514
  • 1
  • 5
  • 16
  • I have tried those solutions and I still get the same result. I think that has something to do that messages properties file require a different approach. – Deniss M. Dec 01 '16 at 10:07
-1

First Of all , clear about configuration setting of Spring Boot.

In POM.XML, Confirm once Layout as "ZIP"

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>  <!-- added -->
                <layout>ZIP</layout> <!-- to use PropertiesLaunchar -->
            </configuration>
        </plugin>
    </plugins>
</build>

Because ,For the PropertiesLauncher the layout is "ZIP" and make sure avoid to use @EnableAutoConfiguration.

or Use @PropertySource annotation for external properties. ( Refer :Spring Boot PropertySource).

This Topic already discussed by StackOverflow.

Refer :

Spring Boot: Is it possible to use external application.properties files in arbitrary directories with a fat jar?

Spring @Configuration file with PropertyPlaceholderConfigurer bean doesn't resolve @Value annotation

Community
  • 1
  • 1
maha lakshmi
  • 57
  • 1
  • 2
  • 9
  • 2
    There is nothing about application.properties in my post. I am asking about messages.properties. This is different. Application.properties work fine with my current setup without the mentioned adjustments from your side. – Deniss M. Dec 01 '16 at 11:48