SpringBoot - 配置檔案application.properties使用詳解(附:Profile多環境配置)
阿新 • • 發佈:2021-12-13
1,開啟自動轉碼功能
application.properties提供了自定義屬性的支援,如果資料有中文的話需要進行轉碼,否則可能會出現亂碼問題。
如果我們使用的是IntelliJ IDEA,那麼直接在setting配置中進行如下設定,這樣編輯器就會自動對中文內容進行轉碼。
2,配置屬性的定義
(1)我們可以在application.properties中新增類似如下這樣簡單的常量配置:my.name=小明
my.age=25
(2)配置屬性之間也可以相互引用使用:
my.name=小明
my.age=25
my.info=name:${my.name} age:${my.age}
(3)配置檔案中可以使用${random}來生成各種不同型別的隨機值:
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}
3,將資料注入到屬性上
(1)在需要的地方我們使用@Value註解就可以將資料注入到屬性上:
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @Value("${my.name}") String name; @GetMapping("/hello") public String hello() { return "welcome to " + name; } }
(2)執行結果如下:
4,將資料注入到 Bean 中
有時候屬性太多了,一個個繫結到屬性欄位上太麻煩,官方提倡繫結一個物件的bean。
(1)首先我們建立一個名為My的Bean,並將前面的配置資料注入到這個Bean中。
(1)@ConfigurationProperties 中的 prefix 屬性描述了要載入的配置檔案的字首。 (2)Spring Boot 採用了一種寬鬆的規則來進行屬性繫結: 假設 Bean 中的屬性名為 authorName,那麼配置檔案中的屬性可以是 my.author_name、my.author-name、my.authorName 或者 my.AUTHORNAME
程式碼如下
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "my") public class My { private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
(2)然後我們在Controller中引入這個Bean使用即可:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class HelloController { @Autowired My my; @GetMapping("/hello") public String hello() { return my.getName() + " : " + my.getAge(); } }
(3)執行結果如下:
5,使用自定義的配置檔案
(1)有時候我們不希望把所有配置都放在application.properties裡面,這時候我們可以另外定義一個。假設我們自定義的配置檔案是test.properties,放在src/main/resources下面。 (2)新建一個bean類後,通過如下方式將這個自定義的配置檔案資料注入進來:import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "my") @PropertySource("classpath:test.properties") public class My { private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
6,使用命令列引數進行配置
(1)在命令列中通過java -jar命令啟動專案時,可以使用連續的兩個減號--對application.properties中的屬性值進行賦值。 (2)比如下面命令修改tomcat埠號為8081。其等價於在application.properties中新增屬性server.port=8081: 注意:如果application.properties中已經有同名屬性,那麼命令列屬性會覆蓋application.properties的屬性。java -jar xx.jar --server.port=8081
7,配置檔案的優先順序
(1)Spring Boot專案中的application.properties配置檔案一共可以出現在如下4個位置(優先順序逐漸降低):- 專案根目錄下的config資料夾
- 專案根目錄下
- classpath下的config資料夾
- classpath下
8,載入外部的配置檔案
(1)專案打包好以後,我們可以使用命令列引數的形式,啟動專案的時候來指定外部配置檔案的位置。
java -jar xxx.jar --spring.config.location=/Volumes/BOOTCAMP/application.properties
(2)當然我們也可以指定外部配置所在的資料夾,啟動時會搜尋並使用該資料夾下的配置檔案:
java -jar xxx.jar --spring.config.location=/Volumes/BOOTCAMP/
(3)我們還可以同時配置多個路徑,比如下面樣例先載入外部配置檔案,如果不存在外部配置檔案的話則使用包內預設的配置檔案:
java -jar xxx.jar --spring.config.location=/Volumes/BOOTCAMP/application.properties,classpath:/,classpath:/config/
附:使用 Profile 實現多環境配置
我們在專案釋出之前,一般需要頻繁地在開發環境、測試環境以及生產環境之間進行切換,這個時候大量的配置需要頻繁更改(比如資料庫配置、redis 配置、mongodb 配置等等)。
Spring Boot 的 Profile 就給我們提供瞭解決方案,它約定不同環境下的配置檔名稱規則為:
application-{profile}.properties,其中 {profile} 表示當前環境的名稱。
1,建立配置檔案
(1)首先在resources目錄下建立兩個配置檔案:application-dev.properties和application-prod.properties,分別表示開發環境中的配置和生產環境中的配置。(2)它們兩個分別設定不同的埠號。
#application-dev.properties server.port=8080 #application-prod.properties server.port=80
2,在 application.properties 中配置環境
(1)假設我們在application.properties中進行如下配置,則表示使用application-dev.properties配置檔案啟動專案。spring.profiles.active=dev
(2)如果將dev改為prod,則表示使用application-prod.properties啟動專案。
spring.profiles.active=prod
(3)專案啟動成功後,就可以通過相應的埠進行訪問了。
3,在程式碼中配置環境
(1)除了像前面那樣在application.properties中新增配置,我們也可以在程式碼中新增配置來完成。 (2)比如我們在啟動類的main方法上新增如下程式碼,表示使用application-dev.properties配置檔案啟動專案。import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplicationBuilder builder = new SpringApplicationBuilder(DemoApplication.class); builder.application().setAdditionalProfiles("dev"); builder.run(args); } }
4,在專案啟動時配置環境
我們也可以在專案打包成jar包後啟動時,在命令列中動態指定當前環境:
java -jar xxx.jar --spring.profiles.active=dev
import
org.springframework.boot.autoconfigure.SpringBootApplication;
import
org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public
class
DemoApplication {
public
static
void
main(String[] args) {
SpringApplicationBuilder builder =
new
SpringApplicationBuilder(DemoApplication.
class
);
builder.application().setAdditionalProfiles(
"dev"
);
builder.run(args);
}
}
早年同窗始相知,三載瞬逝情卻萌。年少不知愁滋味,猶讀紅豆生南國。別離方知相思苦,心田紅豆根以生。