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.
1 2 |
@PropertySources({ @PropertySource(value = "classpath:fruits.properties"), @PropertySource(value = "classpath: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(',')}") private List<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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.javabycode.spring.core; import java.util.List; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySources; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Configuration @PropertySources({ @PropertySource(value = "classpath:fruits.properties"), @PropertySource(value = "classpath:country.properties") }) public class MessageProperties { @Value("#{'${fruits.names}'.split(',')}") private List<String> fruits; @Value("#{'${countries.names}'.split(',')}") private List<String> countries; @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } public List<String> getCountries() { return countries; } public List<String> getFruits() { return fruits; } } |
Demo Spring Application
We create main class and get the AppConfig class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javabycode.spring.core; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MySpringApplication { public static void main(String... args){ ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); MessageProperties msgProperty = context.getBean(MessageProperties.class); System.out.println("Fruits: " + msgProperty.getFruits()); System.out.println("Countries: " + msgProperty.getCountries()); } } |
Run the main above, the console prints the following message:
1 2 |
Fruits: [Lemon, Banana, Apple, Mango, Orange] Countries: [France, USA, Colombia, England, Australia] |
References
@PropertySource JavaDoc
@Value JavaDoc
Download complete source code, click link below
spring-@propertysource-multiple-files.zip (314 downloads)