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

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);
hydra
zeus
100
102
103

References

  1. Sping IO – Spring Expression

上一篇: How to pass System Properties in web.xml
下一篇: Spring @Value default value
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号