1. 程式人生 > >Spring-boot中讀取核心配置檔案application和自定義properties配置檔案的方式

Spring-boot中讀取核心配置檔案application和自定義properties配置檔案的方式

前言:瞭解過spring-Boot這個技術的,應該知道Spring-Boot的核心配置檔案application.properties,當然也可以通過註解自定義配置檔案**.properties的資訊。

一.讀取核心配置檔案資訊application.properties的內容

     核心配置檔案是指在resources根目錄下的application.properties或application.yml配置檔案,讀取這兩個配置檔案的方法有兩種,都比較簡單。

(1)核心配置檔案application.properties內容如下:

  1. test.msg=Hello World SpringBoot  

(1)方式一:使用@Value方式(常用)

  1. package Solin.controller;  
  2. import org.springframework.beans.factory.annotation.Value;  
  3. import org.springframework.web.bind.annotation.RequestMapping;  
  4. import org.springframework.web.bind.annotation.RestController;  
  5. @RestController
  6. publicclass WebController {  
  7.     @Value
    ("${test.msg}")  
  8.     private String msg;  
  9.     @RequestMapping("/index1")   
  10.     public String index1(){  
  11.         return"方式一:"+msg;  
  12.     }  
  13. }
注意:在@Value的${}中包含的是核心配置檔案中的鍵名。在Controller類上加@RestController表示將此類中的所有檢視都以JSON方式顯示,類似於在檢視方法上加@ResponseBody。
訪問:http://localhost:8088/index1時得到:"方式一:Hello World SpringBoot"

(2)方式二:使用Environment方式

  1. package Solin.controller;  
  2. import org.springframework.beans.factory.annotation.Autowired;  
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.core.env.Environment;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import org.springframework.web.bind.annotation.RestController;  
  7. @RestController
  8. publicclass WebController {  
  9.     @Autowired
  10.     private Environment env;  
  11.     @RequestMapping("/index2")   
  12.     public String index2(){  
  13.         return"方式二:"+env.getProperty("test.msg");  
  14.     }  
  15. }  
注意:這種方式是依賴注入Evnironment來完成,在建立的成員變數private Environment env上加上@Autowired註解即可完成依賴注入,然後使用env.getProperty("鍵名")即可讀取出對應的值。

訪問:http://localhost:8088/index2時得到:"方式二:Hello World SpringBoot"

(2)核心配置檔案application-dev.yml內容如下:

  1. # --**銀行配置檔案路徑 start
    czb:
      configPath: /usr/local/czbConfig/config.properties
    # --浙商銀行相關配置檔案路徑 end
application*.yml核心配置檔案,也是用上面兩種方式讀取其內容。例如:如下圖使用方式一來讀取:



二.讀取自定義配置檔案資訊,例如:author.properties

為了不破壞核心檔案的原生態,但又需要有自定義的配置資訊存在,一般情況下會選擇自定義配置檔案來放這些自定義資訊,這裡在resources目錄下建立配置檔案authorA.properties

resources/author/authorA.properties內容如下:

  1. auth.name=Solin  
  2. auth.age=22

建立管理配置的實體類:

  1. package Solin.controller;  
  2. import org.springframework.boot.context.properties.ConfigurationProperties;  
  3. import org.springframework.context.annotation.Configuration;  
  4. import org.springframework.stereotype.Component;  
  5. //加上註釋@Component,可以直接其他地方使用@Autowired來建立其例項物件
  6. @Component
  7. //springboot1.5之前的版本這麼用,1.5版本之後取消了locations屬性,替代方案看最下面。
  8. @ConfigurationProperties(prefix = "auth",locations = "classpath:/author/authorA.properties"
  9. //在啟動類上增加@EnableConfigurationProperties註解,啟用自定義的配置類(重要)
  10. publicclass MyWebConfig{  
  11.     private String name;  
  12.     privateint age;  
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     publicvoid setName(String name) {  
  17.         this.name = name;  
  18.     }  
  19.     publicint getAge() {  
  20.         return age;  
  21.     }  
  22.     publicvoid setAge(int age) {  
  23.         this.age = age;  
  24.     }  
  25. }  

注意:
在@ConfigurationProperties註釋中有兩個屬性:
              locations:指定配置檔案的所在位置
              prefix:指定配置檔案中鍵名稱的字首(我這裡配置檔案中所有鍵名都是以auth.開頭)
    使用@Component是讓該類能夠在其他地方被依賴使用,即使用@Autowired註釋來建立例項。


建立測試Controller

  1. package Solin.controller;  
  2. import org.springframework.beans.factory.annotation.Autowired;  
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.ResponseBody;  
  6. @Controller
  7. publicclass ConfigController {  
  8.     @Autowired
  9.     private MyWebConfig conf;  
  10.     @RequestMapping("/test")   
  11.     public@ResponseBody String test() {  
  12.         return"Name:"+conf.getName()+"---"+"Age:"+conf.getAge();   
  13.     }  
  14. }  

注意:由於在Conf類上加了註釋@Component,所以可以直接在這裡使用@Autowired來建立其例項物件。

訪問:http://localhost:8088/test時得到:"Name:Solin---Age:22"

備註:spring boot1.5以上版本@ConfigurationProperties取消locations註解後的替代方案,只是下面註解變了,別的都一樣。

建立管理配置的實體類:

  1. package Solin.controller;  
  2. import org.springframework.boot.context.properties.ConfigurationProperties;  
  3. import org.springframework.context.annotation.Configuration;  
  4. import org.springframework.stereotype.Component;  
  5. @Component
  6. @ConfigurationProperties(prefix ="auth"
  7. @PropertySource("classpath:/author/authorA.properties")//@PropertySource來指定自定義的資源目錄
  8. //在啟動類上取消掉@EnableConfigurationProperties註解,springboot1.5版本之後不需要@EnableConfigurationProperties註解了
  9. publicclass MyWebConfig{  
  10.     private String name;  
  11.     privateint age;  
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.     publicvoid setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18.     publicint getAge() {  
  19.         return age;  
  20.     }  
  21.     publicvoid setAge(int age) {  
  22.         this.age = age;  
  23.     }  
  24. }  

  1. test.msg=Hello World SpringBoot