Spring @propertysources multiple files example

This Spring @propertysources multiple files example shows you how to load a list from multiple property files using the @Value annotation. In this tutorial we’ll be using the @propertysources annotation which combines with @propertysource annotation to load multiple property files. In fact, you need this to store your inequivalent property configurations to the separated files.

Let’s begin:

In this Spring @propertysources multiple files example, we have two property files and are located on the class path, like below:



Spring @Propertysources Loading Multiple Property Files
If there are multiple PropertySources, you can use annotation @PropertySources which aggregates several PropertySource annotations. In each @PropertySource, you can specify one or more resource locations. Here, we have two property files such as fruits.properties and country.properties.

Spring @Value Loading List from Properties
Now, we can load a list from a property file by annotating the field with the @Value annotation and using the split() method.

Full example for @PropertySources and @Value annotation
We create MessageProperties class which is annotated @PropertySources annotation to load multiple resource properties and use @Value annotation to inject a list with a property.

Demo Spring Application
We create main class and get the AppConfig class.

Run the main above, the console prints the following message:

@PropertySource JavaDoc
@Value JavaDoc

Download complete source code, click link below

spring-@propertysource-multiple-files.zip (10 downloads)

Leave a Comment


Please share it if you found this useful
Hide Buttons