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);
}
}