简介
本文介绍SpringBoot的配置文件与Java转换时数据类型的对应关系。
引用
spring: application: name: storage cloud: config: name: ${spring.application.name}
数组
法1:逗号
applicaiton.yml
单行写法
test: myList: Tony,Pepper
多行写法
test: myList: " Tony, Peter "
代码
@Value("${test.myList}") private List<String> lastNames;
或者:
@Value("#{'${test.myList}'.split(',')}") private List<String> lastNames;
注意:以上List<String> 完全可以换成String[]、Set<String>。
取到的结果都是下边这个(自动以,分割,并去除空白字符):
[ "Tony", "Peter" ]
法2:并列的“-”
applicaiton.yml
test: myList: - Tony - Pepper
代码(成功的)
package com.example.tmp; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.util.List; @Data @Component @ConfigurationProperties(prefix = "test") public class TestProperties { private List<String> mylist; @PostConstruct public void print() { System.out.println(mylist); } }
运行结果
[Tony, Pepper]
代码(失败的)
package com.example.tmp; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.util.List; @Component public class TestConfig { @Value("${test.mylist}") private List<String> mylist; @PostConstruct public void print() { System.out.println(mylist); } }
这样启动会直接报错:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘test.mylist’ in value “${test.mylist}”
Map
法1:对象
application.yml
test: myMap: lastName: Tony firstName: Stark
代码
package com.example.tmp; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.util.Map; @Data @Component @ConfigurationProperties(prefix = "test") public class TestProperties { private Map<String, String> myMap; @PostConstruct public void print() { System.out.println(myMap); } }
执行结果
{lastName=Tony, firstName=Stark}
注意
如果application.yml如下:
test: myMap: "19:00:00": 5 "19:30:00": 6
打印结果为:
{190000=5, 193000=6}
暂时没找到原因,如果要用时间,可以用数组包含对象的方式:见下边“数组包含对象”
法2:对象放到字符串中
application.yml
test: myMap: '{ lastName: "Tony", firstName: "Stark", code: "01" }'
代码
@Value("#{${test.myMap}}") Map<String, String> map;
注意
1. 对于01,外边必须有引号
否则识别出来之后,只有1,少了0
2. application.yml的对象必须放到引号里
下边几种方法都是不可以的:
test: myMap: lastName: "Tony", firstName: "Stark", code: 01
test: myMap: { lastName: "Tony", firstName: "Stark", code: 01 }
3.key不能有横线,可以有下划线 。
比如:last-name不可以,last_name可以。
数组包含对象
application.yml
test: myMap: - time: "19:00:00" delay: 5 - time: "19:30:00" delay: 6
或者
test:
myMap:
- {time: "19:00:00", delay: 5}
- {time: "19:30:00", delay: 6}
代码
package com.example.tmp; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.util.List; import java.util.Map; @Data @Component @ConfigurationProperties(prefix = "test") public class MyBean { private List<Map<String, String>> myMap; @PostConstruct public void print() { System.out.println(myMap); } }
执行结果
[{delay=5, time=19:00:00}, {time=19:30:00, delay=6}]
或者这么写:
package com.example.tmp; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.util.List; @Data @Component @ConfigurationProperties(prefix = "test") public class MyBean { private List<Program> myMap; @PostConstruct public void print() { System.out.println(myMap); } @Data private static class Program{ private String time; private Integer delay; } }
结果
[MyBean.Program(time=19:00:00, delay=5), MyBean.Program(time=19:30:00, delay=6)]
请先
!