Spring-boot中讀取核心配置檔案application和自定義properties配置檔案的方式
前言:瞭解過spring-Boot這個技術的,應該知道Spring-Boot的核心配置檔案application.properties,當然也可以通過註解自定義配置檔案**.properties的資訊。
一.讀取核心配置檔案資訊application.properties的內容
核心配置檔案是指在resources根目錄下的application.properties或application.yml配置檔案,讀取這兩個配置檔案的方法有兩種,都比較簡單。
(1)核心配置檔案application.properties內容如下:
-
test.msg=Hello World SpringBoot
(1)方式一:使用@Value方式(常用)
- package Solin.controller;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- publicclass WebController {
-
@Value
- private String msg;
- @RequestMapping("/index1")
- public String index1(){
- return"方式一:"+msg;
- }
- }
訪問:http://localhost:8088/index1時得到:"方式一:Hello World SpringBoot"
(2)方式二:使用Environment方式
- package Solin.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.core.env.Environment;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- publicclass WebController {
- @Autowired
- private Environment env;
- @RequestMapping("/index2")
- public String index2(){
- return"方式二:"+env.getProperty("test.msg");
- }
- }
訪問:http://localhost:8088/index2時得到:"方式二:Hello World SpringBoot"
(2)核心配置檔案application-dev.yml內容如下:
-
# --**銀行配置檔案路徑 start
czb:
configPath: /usr/local/czbConfig/config.properties
# --浙商銀行相關配置檔案路徑 end
二.讀取自定義配置檔案資訊,例如:author.properties
為了不破壞核心檔案的原生態,但又需要有自定義的配置資訊存在,一般情況下會選擇自定義配置檔案來放這些自定義資訊,這裡在resources
目錄下建立配置檔案authorA.properties
resources/author/authorA.properties
內容如下:
- auth.name=Solin
- auth.age=22
建立管理配置的實體類:
- package Solin.controller;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.stereotype.Component;
-
//加上註釋@Component,可以直接其他地方使用@Autowired來建立其例項物件
- @Component
- //springboot1.5之前的版本這麼用,1.5版本之後取消了locations屬性,替代方案看最下面。
- @ConfigurationProperties(prefix = "auth",locations = "classpath:/author/authorA.properties")
- //在啟動類上增加@EnableConfigurationProperties註解,啟用自定義的配置類(重要)
- publicclass MyWebConfig{
- private String name;
- privateint age;
- public String getName() {
- return name;
- }
- publicvoid setName(String name) {
- this.name = name;
- }
- publicint getAge() {
- return age;
- }
- publicvoid setAge(int age) {
- this.age = age;
- }
- }
注意:
在@ConfigurationProperties註釋中有兩個屬性:
locations:指定配置檔案的所在位置
prefix:指定配置檔案中鍵名稱的字首(我這裡配置檔案中所有鍵名都是以auth.開頭)
使用@Component是讓該類能夠在其他地方被依賴使用,即使用@Autowired註釋來建立例項。
建立測試Controller
- package Solin.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- @Controller
- publicclass ConfigController {
- @Autowired
- private MyWebConfig conf;
- @RequestMapping("/test")
- public@ResponseBody String test() {
- return"Name:"+conf.getName()+"---"+"Age:"+conf.getAge();
- }
- }
注意:由於在Conf類上加了註釋@Component,所以可以直接在這裡使用@Autowired來建立其例項物件。
訪問:http://localhost:8088/test時得到:"Name:Solin---Age:22"
備註:spring boot1.5以上版本@ConfigurationProperties取消locations註解後的替代方案,只是下面註解變了,別的都一樣。
建立管理配置的實體類:
- package Solin.controller;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.stereotype.Component;
- @Component
- @ConfigurationProperties(prefix ="auth")
-
@PropertySource("classpath:/author/authorA.properties")//
@PropertySource
來指定自定義的資源目錄 - //在啟動類上取消掉@EnableConfigurationProperties註解,springboot1.5版本之後不需要@EnableConfigurationProperties註解了。
- publicclass MyWebConfig{
- private String name;
- privateint age;
- public String getName() {
- return name;
- }
- publicvoid setName(String name) {
- this.name = name;
- }
- publicint getAge() {
- return age;
- }
- publicvoid setAge(int age) {
- this.age = age;
- }
- }
- test.msg=Hello World SpringBoot