玩轉Spring Boot 自定義配置、導入XML配置與外部化配置


玩轉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中加入以下依賴:
  1. <dependency>    
  2.           <groupId>org.springframework.boot</groupId>    
  3.           <artifactId>spring-boot-configuration-processor</artifactId>    
  4.           <optional>true</optional>    
  5. </dependency>   

第一種:
     (1) 在src/main/resources下新建application.properties文件並加入以下代碼:
  1. file.upload.stor-path=E:/test/  
      (2)直接使用@Value注解方式,具體代碼如下:
  1. package com.chengli.springboot.helloworld;  
  2.   
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.boot.SpringApplication;  
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8.   
  9. @RestController  
  10. @SpringBootApplication  
  11. public class SampleController {  
  12.     @Value(value = "${file.upload.stor-path}")  
  13.     private String storPath;  
  14.   
  15.     @RequestMapping("/")  
  16.     String home() {  
  17.         return "Hello World! file.upload.stor-path為:" + storPath;  
  18.     }  
  19.   
  20.     public static void main(String[] args) throws Exception {  
  21.         SpringApplication springApplication = new SpringApplication(SampleController.class);  
  22.         springApplication.run(args);  
  23.     }  
  24. }  
第二種:

      屬性配置放在application.properties文件中,使用@ConfigurationProperties將配置屬性注入到Bean中,代碼如下:
      (1)定義FileUploadProperties類
  1. package com.chengli.springboot.helloworld;  
  2.   
  3. import org.springframework.boot.context.properties.ConfigurationProperties;  
  4. import org.springframework.stereotype.Component;  
  5.   
  6. @Component  
  7. @ConfigurationProperties(prefix = "file.upload")  
  8. public class FileUploadProperties {  
  9.     private String storPath;  
  10.   
  11.     public String getStorPath() {  
  12.         return storPath;  
  13.     }  
  14.   
  15.     public void setStorPath(String storPath) {  
  16.         this.storPath = storPath;  
  17.     }  
  18. }  
      (2)入口啟動類代碼如下:

  1. package com.chengli.springboot.helloworld;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.boot.SpringApplication;  
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8.   
  9. @RestController  
  10. @SpringBootApplication  
  11. public class SampleController {  
  12.     @Autowired  
  13.     private FileUploadProperties fileUploadProperties;  
  14.   
  15.     @RequestMapping("/")  
  16.     String home() {  
  17.         return "Hello World! file.upload.stor-path為:" + fileUploadProperties.getStorPath();  
  18.     }  
  19.   
  20.     public static void main(String[] args) throws Exception {  
  21.         SpringApplication springApplication = new SpringApplication(SampleController.class);  
  22.         springApplication.run(args);  
  23.     }  
  24. }  
      注意:這里我對FileUploadProperties使用了@Component注解,如果沒有使用@Component注解,則需要在入口啟動類上加上@EnableConfigurationProperties注解。Spring Boot 在properties文件中支持使用SpEL表達式,可以進行校驗(校驗注解使用的是javax.validation)等操作。

例如以下:
(1)隨機數:
          test.int.random=${random.int}
(2)數組注入
          test.int.random[0]=${random.int}
          test.int.random[1]=${random.int}
(3)校驗
         @NotNull
         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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM