180601-SpringBoot读取properties中list和map数据

SpringBoot读取properties中list和map数据

自定义citycode.properties

  1. 自定义citycode.properties中list类型

    1
    2
    3
    4
    5
    #List properties
    citycode.list[0]=www
    citycode.list[1]=localhost
    citycode.list[2]=wuhan
    citycode.list[3]=tianjin
  2. 自定义citycode.properties中map类型

    1
    2
    3
    4
    5
    #Map Properties
    citycode.map.www=4201
    citycode.map.wuhan=4201
    citycode.map.tianjin=1200
    `

编写代码读取properties文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@Configuration
@PropertySource("classpath:citycode.properties")
@ConfigurationProperties(prefix = "citycode")
public class CityCodeConfig {

private List<String> list = new ArrayList<>();

private Map<String, String> map = new HashMap<>();

}

代码中使用

1
2
@Autowired
private CityCodeConfig cityCodeConfig;

参考文档

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×