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:

country.properties

1

countries.names=France,USA,Colombia,England,Australia

fruits.properties

1

fruits.names=Lemon,Banana,Apple,Mango,Orange

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.

1

2

@Value("#{'${fruits.names}'.split(',')}")

privateList<String>fruits;

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.