目錄
- 一、核心配置格式
- 1. .properties 檔案(默認采用該檔案)
- 2. .yml 檔案
- 二、多環境配置
- 1. 步驟如下
- 三、Spring Boot 自定義配置
- 1. @Value 注解
- 2. @ConfigurationProperties
- 3. 警告解決
- 4. 中文亂碼
Spring Boot 的核心組態檔用于配置 Spring Boot 程式,名字必須以 application 開始
一、核心配置格式
1. .properties 檔案(默認采用該檔案)
在 02-springboot-springmvc 專案基礎上,進行修改,也就是在上一個專案上
專案名稱為:03-springboot-port-context-path
通過修改 application.properties 組態檔,在修改默認 tomcat 埠號及專案背景關系件根鍵值對的 properties 屬性檔案配置方式
#設定內嵌的Tomcat的埠號
server.port=9090
#配置專案的背景關系根
server.servlet.context-path=/03-springboot-port-context-path
配置完畢之后,啟動,瀏覽器測驗

頁面顯示結果

2. .yml 檔案
專案名稱:04-springboot-yml在03的專案基礎上
為了區分,把SpringbootController稍作修改
package com.md.springboot.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author MD
* @create 2020-08-20 16:40
*/
@Controller
public class SpringbootController {
@RequestMapping(value = https://www.cnblogs.com/mengd/p/"/say")
@ResponseBody
public String say(String name){
return "hello " + name;
}
}
yml 是一種 yaml 格式的組態檔,主要采用一定的空格、換行等格式排版進行配置,yaml 是一種直觀的能夠被計算機識別的的資料序列化格式,容易被人類閱讀,yaml 類似于 xml,但是語法比 xml 簡潔很多,值與前面的冒號配置項必須要有一個空格, yml 后綴也可以使用 yaml 后綴
在resources下把application.properties洗掉掉,新建一個 application.yml

然后啟動,通過瀏覽器進行訪問,也是可以的

注意:
當兩種格式組態檔同時存在,使用的是.properties 組態檔
二、多環境配置
在實際開發的程序中,我們的專案會經歷很多的階段(開發->測驗->上線),每個階段的配置也會不同,例如:埠、背景關系根、資料庫等,那么這個時候為了方便在不同的環境之間切換,SpringBoot 提供了多環境配置,具體步驟如下
1. 步驟如下
專案名稱05-springboot-multi-environment,在上一個的基礎上
為每個環境創建一個組態檔,命名必須以 application- 環境標識.properties|yml

其中application-dev.properties
#開發環境
#設定內嵌的Tomcat的埠號
server.port=8080
#配置專案的背景關系根
server.servlet.context-path=/05-springboot-multi-environment
application-product.properties
#生成環境
#設定內嵌的Tomcat的埠號
server.port=8081
#配置專案的背景關系根
server.servlet.context-path=/05-springboot-multi-environment
application-test.properties
#測驗環境
#設定內嵌的Tomcat的埠號
server.port=8082
#配置專案的背景關系根
server.servlet.context-path=/05-springboot-multi-environment
在總組態檔 application.properties 進行環境的激活
#總組態檔
#激活開發環境
#spring.profiles.active=dev
#激活測驗環境
#spring.profiles.active=test
#激活生產環境
spring.profiles.active=product
等號右邊的值和組態檔的環境標識名一致 , 可以更改總組態檔的配置
也就是等號右邊的值和組態檔和“-”后面值一樣,就是使用的那種方式
運行,啟動,測驗

如果是以yml結尾的組態檔,和這個是類似的,這里就不詳細說了

三、Spring Boot 自定義配置
在 SpringBoot 的核心組態檔中,除了使用內置的配置項之外,我們還可以在自定義配置,然后采用如下注解去讀取配置的屬性值
1. @Value 注解
在專案06-springboot-custom中,創建步驟就不詳細說了,之前寫的有
在核心組態檔application.properties中,添加兩個自定義配置項 school.name 和web,在 IDEA 中可以看到這兩個屬性不能被 SpringBoot 識別,背景是桔色的
server.port=9090
server.servlet.context-path=/
#設定自定義的
school.name=mit
web=http://www.baidu.com
在 SpringBootController 中定義屬性,并使用@Value 注解,并對其方法進行測驗
package com.md.springboot.web;
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;
/**
* @author MD
* @create 2020-08-20 17:48
*/
@Controller
public class SpringBootController {
@Value("${school.name}")
private String schoolName;
@Value("${web}")
private String web;
@RequestMapping(value = https://www.cnblogs.com/mengd/p/"/say")
@ResponseBody
public String say(){
return schoolName+"-------"+web;
}
}
啟動,測驗

2. @ConfigurationProperties
專案名稱:07-springboot-custom
將整個檔案映射成一個物件,用于自定義配置項比較多的情況
在 com.md.springboot.config 包下創建 ConfigInfo 類,
并為該類加上 Component 和ConfigurationProperties 注解,并在 ConfigurationProperties 注解中添加屬性 prefix,作用可以區分同名配置
package com.md.springboot.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author MD
* @create 2020-08-20 17:56
*/
// 將此類交給spring容器進行管理
@Component
@ConfigurationProperties(prefix = "school")
public class ConfigInfo {
private String name;
private String web;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWeb() {
return web;
}
public void setWeb(String web) {
this.web = web;
}
}
application.properties 組態檔
server.port=9090
server.servlet.context-path=/
#設定自定義的,注意都設定了school開頭,如果還有其他的name,方便區分
school.name=mit
school.web=http://www.jd.com
在 SpringBootController 中注入 ConfigInfo 配置類
此時的使用方法和value的不同,注意區別
package com.md.springboot.web;
import com.md.springboot.config.ConfigInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author MD
* @create 2020-08-20 17:59
*/
@Controller
public class SpringBootController {
// 自動注入
@Autowired
private ConfigInfo configInfo;
@RequestMapping(value = https://www.cnblogs.com/mengd/p/"/config")
public @ResponseBody String say(){
return configInfo.getName()+"============="+configInfo.getWeb();
}
}
運行,測驗

3. 警告解決
在 ConfigInfo 類中使用了 ConfigurationProperties 注解后,IDEA 會出現一個警告,但不影響程式的執行

如果想不看到這個警告,可以添加一個依賴,在pom.xml中
<!--出現警告-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
這樣就可以了
4. 中文亂碼
如果在 SpringBoot 核心組態檔中有中文資訊,會出現亂碼:
- 一般在組態檔中,不建議出現中文(注釋除外)
- 如果有,可以先轉化為 ASCII 碼

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/49417.html
標籤:Java
