一、@Value方式
1.我的配置文件:application-dev.yml
# 自定义项目配置 startproject: pro1: 11 pro2: 11 pro3: 11 pro4: 11 lists: - ‘1‘ - ‘2‘ - ‘3‘ maps: key1: 1 key2: 2 key3: 3
2.我的Java Bean
/** * @author zhangboqing * @date 2018/9/18 */ @Data @Configuration public class CustomCode { //====================== 项目配置 ============================ @Value("${startproject.pro1}") private String pro1; @Value("${startproject.pro2}") private String pro2; @Value("${startproject.pro3}") private String pro3; @Value("${startproject.pro4}") private String pro4; @Value("${startproject.lists[0]},${startproject.lists[1]},${startproject.lists[2]}") private List<String> lists; // @Value("${startproject.maps2}") 无法获取到 // private Map<String,Object> maps; }
二、@ConfigurationProperties方式 (推荐)
1.我的配置文件:application-dev.yml
# 自定义项目配置startproject: pro1: 11 pro2: 11 pro3: 11 pro4: 11 lists: - ‘1‘ - ‘2‘ - ‘3‘ maps: key1: 1 key2: 2 key3: 3 maps2: {key1: 1,key2: 2,key3: 3}
2.我的Java Bean
/** * @author zhangboqing * @date 2018/9/18 */ @Data @Configuration @ConfigurationProperties(prefix = "startproject") public class CustomCode { //====================== 项目配置 ============================ private String pro1; private String pro2; private String pro3; private String pro4; private List<String> lists; private Map<String,Object> maps; private Map<String,Object> maps2; }
原文地址:https://www.cnblogs.com/756623607-zhang/p/11192217.html
时间: 2024-11-08 17:50:48