玩轉Spring Boot 自定義配置、導入XML配置與外部化配置
在這里我會全面介紹在Spring Boot里面如何自定義配置,更改Spring Boot默認的配置,以及介紹各配置的優先級順序。Spring Boot使用一個全局的配置文件application.properties,Spring Boot 支持使用YAML語言的配置文件,YAML是以數據位中心的語言,所以使用application.yml作為全局配置也是同樣的效果,
如果使用YAML替代properties注意寫法,冒號后面要加個空格,否則會解析不出來。而且在Spring Boot里面配置名稱支持多種方式,例如:server.ssl.key-store,可以寫成:server.ssl.keyStore都是可以的。下面具體詳細介紹。
1.引用XML文件配置
在實際項目中有的情況需要使用到XML配置文件,或者是你還不習慣用Java 配置的方式,那么你可以通過在入口啟動類上加上@ImportResource(value = { "路徑" })或者使用@ImportResource(locations= { "路徑" }),一樣的效果,多個XML文件的話你可以用逗號“,”分隔,就這樣輕而易舉的引用XML配置。
2.引入多個@Configuration 配置類
在實際項目中可能不會把所有的配置都放在一個配置類(用@Configuration注解的類)中,可能會分開配置。這時可以用@Import注解引用。
3.引用自定義properties
Spring Boot使用全局配置(application.properties)提供了很多的默認的配置屬性。在開發的時候,大多數會用到自定義的一些配置屬性,例如:指定上傳文件保存的路徑,定義:file.upload.stor-path=E:/test/,Spring Boot 提供了@Value注解獲取properties中的屬性,還提供了基於類型安全的配置方式,通過@ConfigurationProperties將properties屬性注入到一個Bean中,在1.4以上版本官方不建議使用@ConfigurationProperties來指定properties文件位置。接下來請看實例:
在pom.xml中加入以下依賴:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
第一種:
(1) 在src/main/resources下新建application.properties文件並加入以下代碼:
- file.upload.stor-path=E:/test/
- package com.chengli.springboot.helloworld;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @SpringBootApplication
- public class SampleController {
- @Value(value = "${file.upload.stor-path}")
- private String storPath;
- @RequestMapping("/")
- String home() {
- return "Hello World! file.upload.stor-path為:" + storPath;
- }
- public static void main(String[] args) throws Exception {
- SpringApplication springApplication = new SpringApplication(SampleController.class);
- springApplication.run(args);
- }
- }
屬性配置放在application.properties文件中,使用@ConfigurationProperties將配置屬性注入到Bean中,代碼如下:
(1)定義FileUploadProperties類
- package com.chengli.springboot.helloworld;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
- @Component
- @ConfigurationProperties(prefix = "file.upload")
- public class FileUploadProperties {
- private String storPath;
- public String getStorPath() {
- return storPath;
- }
- public void setStorPath(String storPath) {
- this.storPath = storPath;
- }
- }
- package com.chengli.springboot.helloworld;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @SpringBootApplication
- public class SampleController {
- @Autowired
- private FileUploadProperties fileUploadProperties;
- @RequestMapping("/")
- String home() {
- return "Hello World! file.upload.stor-path為:" + fileUploadProperties.getStorPath();
- }
- public static void main(String[] args) throws Exception {
- SpringApplication springApplication = new SpringApplication(SampleController.class);
- springApplication.run(args);
- }
- }
例如以下:
(1)隨機數:
test.int.random=${random.int}
(2)數組注入
test.int.random[0]=${random.int}
test.int.random[1]=${random.int}
(3)校驗
@NotNull
private String storPath;
private String storPath;
4.外部化配置(配置方式與優先級)
Spring Boot 允許外化配置,Spring Boot使用了一個特別的PropertySource次序來允許對值進行覆蓋,覆蓋的優先級順序如下:
(1)Devtools全局設置主目錄(~ /.spring-boot-devtools.properties 為活躍的)。
(2)@TestPropertySource注解在Test。
(3)@SpringBootTest#properties 注解在Test。
(4)命令行參數。
(5)從SPRING_APPLICATION_JSON屬性(內聯JSON嵌入在一個環境變量或系統屬性)。
(6)ServletConfig init參數。
(7)ServletContext init參數。
(8)JNDI屬性java:comp/env。
(9)Java系統屬性(System.getProperties())。
(10)操作系統環境變量。
(11)RandomValuePropertySource配置的random.*屬性值
(12)打包在jar以外的application-{profile}.properties或application.yml配置文件
(13)打包在jar以內的application-{profile}.properties或application.yml配置文件
(14)打包在jar以外的application.properties或application.yml配置文件
(15)打包在jar以內的application.properties或application.yml配置文件
(16)@configuration注解類上的@PropertySource。
(17)默認的屬性(使用SpringApplication.setDefaultProperties指定)。
a) 通過命令行來修改默認參數,例如:
啟動命令:java -jar *.jar --name="chengli"
以上的意思是,將name值修改為:chengli
b) 通過命令行來設置加載properties 例如:
java -jar *.jar --spring.profiles.active=dev
這里如果不了解profile的話,后面的文章中會介紹到。
5.application.properties文件按優先級,優先級高的會覆蓋優先級低的
優先級順序如下:
(1)當前目錄下的一個/config子目錄
(2)當前目錄
(3)一個classpath下的/config包
(4)classpath根目錄
有興趣的朋友可以加群探討相互學習:
Spring Boot QQ交流群:599546061
Spring Boot QQ交流群:599546061
