原文地址:http://www.yiidian.com/springboot/springboot-properties.htmlhtml
Spring Boot裏面有兩個註解能夠讀取application.properties或application.yml文件的屬性值。java
1)配置spring
nickname=eric age=20
2)SpringBoot讀取配置數組
/** * @Value註解 - 讀取application.properties屬性 * 一點教程網 - www.yiidian.com */ @Controller public class ConfigController { //1.1 讀取基本類型 @Value("${nickname}") private String nickname; @Value("${age}") private Integer age;
1)配置springboot
user.nickname=jack user.age=18
2)SpringBoot讀取配置app
/** * @Value註解 - 讀取application.properties屬性 * 一點教程網 - www.yiidian.com */ @Controller public class Config2Controller { //1.2 讀取JavaBean類型 @Value("${user.nickname}") private String nickname; @Value("${user.age}") private Integer age; ``` ### 1.3 數組/List集合 1)配置 ```properties user.list=eric,jack,rose
2)SpringBoot讀取配置yii
/** * @Value註解 - 讀取application.properties屬性 * 一點教程網 - www.yiidian.com */ @Controller public class Config3Controller { //1.3 讀取數組/List集合類型 @Value("#{'${user.list}'.split(',')}") private List<String> list;
1)配置學習
user.map={nickname:'eric',age:20}
2)SpringBoot讀取配置網站
/** * @Value註解 - 讀取application.properties屬性 * 一點教程網 - www.yiidian.com */ @Controller public class Config4Controller { //1.4 讀取Map集合類型 @Value("#{${user.map}}") private Map<String,Object> map;
1)配置this
nickname=eric age=20
2)SpringBoot讀取配置
/** * @ConfigurationProperties註解 - 讀取application.properties屬性 * 一點教程網 - www.yiidian.com */ @Controller @ConfigurationProperties(prefix = "") public class Config5Controller { //1.1 讀取基本類型 private String nickname; private Integer age; public void setNickname(String nickname) { this.nickname = nickname; } public void setAge(Integer age) { this.age = age; }
注意如下幾點:
1)配置
user.nickname=jack user.age=18
2)SpringBoot讀取配置
/** * @ConfigurationProperties註解 - 讀取application.properties屬性 * 一點教程網 - www.yiidian.com */ @Controller @ConfigurationProperties(prefix = "user") public class Config6Controller { //1.2 讀取JavaBean類型 private String nickname; private Integer age; public void setNickname(String nickname) { this.nickname = nickname; } public void setAge(Integer age) { this.age = age; }
1)配置
user.list2[0]=eric user.list2[1]=jack user.list2[2]=rose
2)SpringBoot讀取配置
/** * @ConfigurationProperties註解 - 讀取application.properties屬性 * 一點教程網 - www.yiidian.com */ @Controller @ConfigurationProperties(prefix = "user") public class Config7Controller { //1.3 讀取數組/List集合類型 private List<String> list2; public void setList2(List<String> list2) { this.list2 = list2; }
1)配置
user.map2.nickname=eric user.map2.age=20 ```properties 2)SpringBoot讀取配置 ```java /** * @ConfigurationProperties註解 - 讀取application.properties屬性 * 一點教程網 - www.yiidian.com */ @Controller @ConfigurationProperties(prefix = "user") public class Config8Controller { //1.4 讀取Map集合類型 private Map<String,Object> map2; public void setMap2(Map<String, Object> map2) { this.map2 = map2; }
源碼下載:https://pan.baidu.com/s/1eAwxjQClVt3TxuqtL6VbIA
歡迎關注個人公衆號::一點教程。得到獨家整理的學習資源和平常乾貨推送。 若是您對個人系列教程感興趣,也能夠關注個人網站:yiidian.com