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}