I have a messages_en.properties inside the classpath (src/main/resources) and one outside the jar, in /config folder. But the messages_en.properties(content) in the /config folder doesn't overwrite the content which is inside the classpath, even after adding this tag:
spring.config.location=config/messages_en.properties
Am I going wrong or this is not possible at all in spring boot?
Do note, the application.properties is in the /config folder (externalized configuration).
You're configuring spring.config.location
, which is used to provide the location of the external application configuration (externalized configuration).
If you want to refer to an external location you should prefix your path with file:
, for example:
spring.config.location=file:config/application.properties
However, when you use a file called messages_en.properties it's more likely that this is the properties file used by a MessageSource
(for internationalization/localization) rather than using it as a replacement for your application.properties file.
You can configure an external location for these messages as well, by configuring the spring.messages.*
properties, for example:
spring.messages.basename=file:config/messages
You don't have to add the language code (en
), because that's the convention used by Spring already for detecting the proper messages file.
Depending on the given language when calling the MessageSource
, it will either open messages_en.properties or messages_fr.properties or ... and use messages.properties as a fallback if there is no property found for the provided language.
EDIT: It appears that the MessageSourceAutoConfiguration
only kicks in for classpath resources and you need to have a default fallback messages.properties. If you don't have those, it will not work.
However, you can still use those properties and create a MessageSource
manually using @ConfigurationProperties
:
@Bean
@ConfigurationProperties("spring.messages")
public MessageSource messageSource() {
return new ReloadableResourceBundleMessageSource();
}