【Android免費音樂下載app】【佳語音樂下載】建議最少2.0.3版本。最新版本:
https://gitlab.com/gaopinqiang/checkversion/raw/master/Music_Download.apk
以下代碼是在2.3.0.RELEASE版本中使用的
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
一、Spring boot 的核心配置文件
Spring boot的核心配置文件用於配置Spring boot程序,有兩種格式的配置文件(application.properties 、 application.yml):
1、.properties文件
鍵值對的properties屬性文件配置方式
例如:
#配置服務器端口
server.port=9800
#配置應用訪問路徑,新版本這個配置不生效了
server.context-path=/0-springboot-web
server.servlet.context-path=/0-springboot-web
2、.yml文件(值與前面的冒號配置項必須要有一個空格;)
yml 是一種 yaml 格式的配置文件,主要採用一定的空格、換行等格式排版進行配置;
yaml 是一種直觀的能夠被計算機識別的的數據序列化格式,容易被人類閱讀,yaml 類似於 xml,但是語法比 xml 簡潔很多;
yml 後綴也可以使用 yaml 後綴;
例如:
server:
port: 9091
context-path: /0-springboot-web
二、多環境配置
在application.properties中配置
#比如配置測試環境
spring.profiles.active=dev
#比如配置生產環境
spring.profiles.active=product
2個環境的配置文件
application-dev.properties
application-product.properties
特別說明:
- a.如果application.properties和application.yml文件同時存在,優先使用properties裏面的配置。
比如properties配置了port爲9000,yml配置了port爲8080,最後啓動9000。如果properties沒配置,那麼就使用yml配置。 - b.我們可以把相同的配置都放在application.properties中。差異性的配置放在不同的環境配置文件中。
三、Spring boot 自定義配置(2種方式讀取)
我們可以在Spring boot的核心配置文件中自定義配置,然後採用如下註解去讀取配置的屬性值;
1、@Value註解
用於逐個讀取自定義的配置,比如:
@Value("${boot.name}")
private String name;
示例:
在application.properties中配置
#自定義配置文件,如果需要使用需要自己讀取下
boot.name=qiang
boot.location=china
寫個Controller
package com.springboot.web.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ConfigInfoController {
@Value("${boot.name}")
private String name;
@Value("${boot.location}")
private String location;
@RequestMapping("/config")
public @ResponseBody String configInfo(){
return name + " || " + location;
}
}
去瀏覽器中請求:http://localhost:8080/config
輸出結果:qiang || china
2、@ConfigurationProperties
用於將整個文件映射成一個對象,比如:
@Component
@ConfigurationProperties(prefix="boot")
public class MyConfig {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
示例:
在application.properties中配置
#自定義配置文件,如果需要使用需要自己讀取下
boot.name=qiang
boot.location=china
創建一個包config,再創建一個類ConfigInfo,在類上加上註解ConfigurationProperties。
package com.springboot.web.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component//把這個類加載到SpringBoot中來,變成SpringBoot的一個組件
@ConfigurationProperties(prefix = "boot")//這個前綴就是我們application.properties中自定義的前綴
public class ConfigInfo {
//屬性就對應了配置中的後綴
private String name ;
private String location;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
使用:
package com.springboot.web.controller;
import com.springboot.web.config.ConfigInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ConfigInfoController {
@Autowired//使用自動裝配註解就可以直接把配置文件讀取到這個對象中了。
private ConfigInfo configInfo;
@RequestMapping("/configProperties")
public @ResponseBody String configInfoProperties(){
return configInfo.getName() + " == " + configInfo.getLocation();
}
}
去瀏覽器中請求:http://localhost:8080/configProperties
輸出結果:qiang == china