Spring @Value – Import a list from properties file

In this tutorial, we will show you how to import a “List” from a properties file, via Spring EL @Value

Tested with :

  1. Spring 4.0.6
  2. JDK 1.7

Spring @Value and List

In Spring @Value, you can use the split() method to inject the ‘List” in one line.

config.properties

server.name=hydra,zeus
server.id=100,102,103
AppConfigTest.java

package com.mkyong.analyzer.test;

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.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@PropertySource(value="classpath:config.properties")
public class AppConfigTest {
	
	@Value("#{'${server.name}'.split(',')}")
	private List<String> servers;
	
	@Value("#{'${server.id}'.split(',')}")
	private List<Integer> serverId;
	
	//To resolve ${} in @Value
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
		return new PropertySourcesPlaceholderConfigurer();
	}

}

Output


	System.out.println(servers.size());
	for(String temp : servers){
		System.out.println(temp);
	}
		
	System.out.println(serverId.size());
	for(Integer temp : serverId){
		System.out.println(temp);
	}

2
hydra
zeus

3
100
102
103

References

  1. Sping IO – Spring Expression

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
9 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
9 Comment authors
aamanGoldestDanielPyBaha????????? ?????????? Recent comment authors
newest oldest most voted
Ed
Guest
Ed

Thank you!

kyu
Guest
kyu

it’s very good article ~~~ thank you

Vijay
Guest
Vijay

Thank you! . simpler approach in retrieving lists from prop files 🙂

Ethan Sun
Guest
Ethan Sun

Thank you very much?Simple but useful!

????????? ??????????
Guest
????????? ??????????

Also you can use Spring’s ConversionService.
@Bean
public static ConversionService conversionService() {
return new DefaultFormattingConversionService();
}

Baha
Guest
Baha

Thank you for this tuto.It’s helpful

DanielPy
Guest
DanielPy

Thank you

Goldest
Guest
Goldest

Just wanted to check how can I read a Map declared in application.properties in my java file using Environment variable?

In my application.properties file I have,
myMap={key1:’value1′,key2:’value2′}

In java controller file I have,
import org.springframework.core.env.Environment;

@Autowired
Environment env;

But below gives me error, cannot convert from String to HashMap
HashMap myMap = env.getProperty(“myMap”);

How to resolve this? Any help is appreciated.

aaman
Guest
aaman

Good but if the env variable does not exist, it will create a list with “,” as element