springboot入門_獲取屬性文件中的值
阿新 • • 發佈:2018-09-10
too type @property color 接收 tools sta version ring
在上一篇文章中,記錄了用springboot實現輸出一個hello world到前臺的程序,本文記錄學習springboot讀取屬性文件中配置信息。
框架屬性文件(application.properties)
創建一個springboot項目,並引入相關依賴,POM文件如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 5 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 6 7 <modelVersion>4.0.0</modelVersion> 8 9 <groupId>org.allen.learn</groupId> 10 <artifactId>springboot_propertiesparam</artifactId> 11<version>0.0.1-SNAPSHOT</version> 12 13 <packaging>war</packaging> 14 15 <!-- Inherit defaults from Spring Boot --> 16 <parent> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter-parent</artifactId> 19<version>2.0.4.RELEASE</version> 20 </parent> 21 22 <!-- Add typical dependencies for a web application --> 23 <dependencies> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-web</artifactId> 27 </dependency> 28 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-devtools</artifactId> 32 <optional>true</optional> 33 </dependency> 34 35 </dependencies> 36 37 </project>
在resources路徑下創建application.properties文件,並寫入我們的屬性名稱和值,內容如:
allen.properties.type=springboot
allen.properties.title=springboot獲取屬性文件值
寫一個class來接收屬性文件中的值,代碼如下:
1 package org.allen.learn.property; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.stereotype.Component; 5 6 @Component 7 @ConfigurationProperties(prefix="allen.properties")//指定前綴是allen.properties 8 public class PropertiesConfig { 9 10 public String type; 11 12 public String title; 13 14 public String getType() { 15 return type; 16 } 17 18 public void setType(String type) { 19 this.type = type; 20 } 21 22 public String getTitle() { 23 return title; 24 } 25 26 public void setTitle(String title) { 27 this.title = title; 28 } 29 30 }
在controller使用我們上邊類中接收到的屬性文件中的值,代碼:
1 package org.allen.learn.controller; 2 3 import java.io.UnsupportedEncodingException; 4 import java.util.HashMap; 5 import java.util.Map; 6 7 import org.allen.learn.property.PropertiesConfig; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RestController; 11 12 @RestController 13 public class PropertiesController { 14 15 @Autowired 16 private PropertiesConfig propertiesConfig; 17 18 @RequestMapping("/c1") 19 public String getProperties1() { 20 Map<String, Object> map = new HashMap<String, Object>(); 21 map.put("屬性type", propertiesConfig.getType()); 22 try { 23 //application.properties 默認編碼格式iso-8859-1,此處中文轉碼 24 map.put("屬性title", new String(propertiesConfig.getTitle().getBytes("iso-8859-1"), "utf-8")); 25 } catch (UnsupportedEncodingException e) { 26 e.printStackTrace(); 27 } 28 return map.toString(); 29 } 30 31 }
啟動項目,在瀏覽器中請求 http://localhost:8080/c1
在頁面上可以看到我們在屬性文件中默認給的值。這種取值方式我們需要創建一個類來關聯值,還有一中方法就是使用@Value來獲取屬性文件中的值,代碼:
1 package org.allen.learn.controller; 2 3 import java.io.UnsupportedEncodingException; 4 import java.util.HashMap; 5 import java.util.Map; 6 7 import org.springframework.beans.factory.annotation.Value; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController; 10 11 @RestController 12 public class ValueController { 13 14 @Value("${allen.properties.type}")//指定取值屬性名 15 private String type; 16 @Value("${allen.properties.title}") 17 private String title; 18 19 @RequestMapping("/c2") 20 public String getProperties1() { 21 Map<String, Object> map = new HashMap<String, Object>(); 22 map.put("value獲取屬性type", type); 23 try { 24 //application.properties 默認編碼格式iso-8859-1,此處中文轉碼 25 map.put("value獲取屬性title", new String(title.getBytes("iso-8859-1"), "utf-8")); 26 } catch (UnsupportedEncodingException e) { 27 e.printStackTrace(); 28 } 29 return map.toString(); 30 } 31 32 }
啟動項目,在瀏覽器中請求 http://localhost:8080/c2 可以在瀏覽器中看到
自定義屬性文件
在resources路徑下創建一個myjdbc.properties文件,並寫入內容,代碼如:
myjdbc.username=root
myjdbc.password=123456
編寫一個class來接收屬性,代碼:
1 package org.allen.learn.property; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.context.annotation.PropertySource; 5 import org.springframework.stereotype.Component; 6 7 @Component 8 @PropertySource(value="classpath:myjdbc.properties")//指定自定義屬性文件 9 @ConfigurationProperties(prefix="myjdbc")//前綴 10 public class MyJdbcProperties { 11 12 private String username; 13 14 private String password; 15 16 public String getUsername() { 17 return username; 18 } 19 20 public void setUsername(String username) { 21 this.username = username; 22 } 23 24 public String getPassword() { 25 return password; 26 } 27 28 public void setPassword(String password) { 29 this.password = password; 30 } 31 32 33 }
創建一個controller來獲取值,代碼:
1 package org.allen.learn.controller; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import org.allen.learn.property.MyJdbcProperties; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController; 10 11 @RestController 12 public class MyPropertiesController { 13 14 @Autowired 15 private MyJdbcProperties myJdbcProperties; 16 17 @RequestMapping("/c3") 18 public String getMyProperties() { 19 Map<String, Object> map = new HashMap<String, Object>(); 20 map.put("myUsername", myJdbcProperties.getUsername()); 21 map.put("myPassword", myJdbcProperties.getPassword()); 22 return map.toString(); 23 } 24 25 }
啟動項目,在瀏覽器中輸入請求地址 http://localhost:8080/c3 瀏覽器中會輸出內容:
下載源碼 https://files.cnblogs.com/files/wlzq/springboot_propertiesparam.zip
springboot入門_獲取屬性文件中的值