首页 > 其他 > 详细

ymal文件自定义属性的使用

时间:2021-07-29 00:52:55      阅读:27      评论:0      收藏:0      [点我收藏+]

ymal文件自定义属性的使用

开发需要再yml文件中配置一些属性,提高程序的可扩展性,只需要修改配置文件,而不需要修改业务代码。

1、创建yml文件

config-attributes:
  value: 345
  valueArray: 1,2,3,4,5,6,7,8,9
  valueList:
    - 1234
    - 344535
  valueMap:
    name: lili
    age: 20
    sex: female
  valueMapList:
    - name: bob
      age: 21
    - name: caven
      age: 31

2、创建对象配置对应的对象

@Component
@Data
@ConfigurationProperties(prefix = "config-attributes")
public class ConfigBean {
    private String value;
    private String[] valueArray;
    private List<String> valueList;
    private HashMap<String,String> valueMap;
    private List<Map<String,String>> valueMapList;
}

3、测试

@SpringBootTest
class SpringbootMybatisPlusApplicationTests {
    @Autowired
    private ConfigBean config;
    @Test
    void test02(){
        System.out.println("config.getValue()::单值::"+config.getValue());
        System.out.println("config.getValueArray()::数组::"+config.getValueArray());
        System.out.println("config.getValueList()::集合::"+config.getValueList());
        System.out.println("config.getValueMapList()::map+list::"+config.getValueMapList());
        System.out.println("config.getValueMap()::map::"+config.getValueMap());
    }
}

结果

config.getValue()::单值::345
config.getValueArray()::数组::[Ljava.lang.String;@1e1b061
config.getValueList()::集合::[1234, 344535]
config.getValueMapList()::map+list::[{name=bob, age=21}, {name=caven, age=31}]
config.getValueMap()::map::{name=lili, age=20, sex=female}

ymal文件自定义属性的使用

原文:https://www.cnblogs.com/baizhoux/p/15073064.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!