SpringBoot讀取配置檔案總結
前言
Spring-Boot的核心配置檔案是application.properties,會預設讀取該配置檔案,當然也可以通過註解自定義配置檔案的資訊。開發中,經常會有一些常量,變動較少,但是我們不能在java程式碼中寫死,這樣每次修改都得去java程式碼中修改,引用地方較多時我們需要修改很多java檔案;所以我們可以集中寫在某個配置檔案中,這樣只用修改配置檔案就好。
pom.xml:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
注意:你在pom.xml中新增如下配置,不加的話當你使用@ConfigurationProperties註解的時候會報Warning(黃色小感嘆號),但是不影響功能實現(強迫症的我看的不爽)。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
原因:spring預設使用yml中的配置,但有時候要用傳統的xml或properties配置,就需要使用spring-boot-configuration-processor了
一.讀取核心配置檔案資訊application.properties的內容
核心配置檔案是指在resources根目錄下的application.properties或application.yml配置檔案,讀取這兩個配置檔案的方法有兩種,都比較簡單。
核心配置檔案application.properties內容如下,這裡有普通的定義屬性,也有內建的函式,如隨機數,隨機字串等:
# 自定義屬性 com.tmall.id=123.45.67.89 # 引數間引用 com.tmall.id.description=This is id:${com.tmall.id} # 隨機字串 com.blog.value=${random.value} # 隨機int com.blog.number=${random.int} # 隨機long com.blog.bignumber=${random.long} # 10以內的隨機數 com.blog.test1=${random.int(10)} # 10-20的隨機數 com.blog.test2=${random.int[10,20]} class.schoolName=china school class.students[0].name=tom class.students[1].name=jack test.msg=Hello World SpringBoot xiao=qiang
方式一:使用@Value方式(常用)
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebController {
@Value("${com.tmall.id}")
private String id;
@Value("${com.tmall.id.description}")
private String description;
@Value("${com.blog.value}")
private String value;
@Value("${com.blog.number}")
private String number;
@Value("${com.blog.bignumber}")
private String bignumber;
@Value("${com.blog.test1}")
private String test1;
@Value("${com.blog.test2}")
private String test2;
@RequestMapping("/index1")
public String index1(){
return "方式一:"+"id->"+id+";description->"+description+";value->"+value
+";number->"+number+";bignumber->"+bignumber+";test1->"+test1+";test2->"+test2;
}
}
注意:在@Value的${}中包含的是核心配置檔案中的鍵名。在Controller類上加@RestController表示將此類中的所有檢視都以JSON方式顯示,類似於在檢視方法上加@ResponseBody。
訪問:http://localhost:8080/index1時得到:“方式一:id->123.45.67.89;description->This is id:123.45.67.89;value->856f604db7c4b03da21b29776dc0d3dc;number->1522979010;bignumber->-8020258677837408781;test1->6;test2->13”
方式二:把某種相關的配置,注入到某個配置類中,比如上面的application.properties中看到class.xx的配置項,想注入到ClassConfig配置類中。還有集合的注入方法
public class StudentConfig {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="class")
public class ClassConfig {
private String schoolName;
private List<StudentConfig> students = new ArrayList<StudentConfig>();
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
public void setStudents(List<StudentConfig> students) {
this.students = students;
}
public List<StudentConfig> getStudents() {
return students;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hui.config.ClassConfig;
@RestController
public class WebController {
@Value("${class.schoolName}")
private String schoolName;
@Autowired
private ClassConfig classConfig;
@RequestMapping("/index2")
public String index1(){
return "方式二:"+"schoolName="+classConfig.getSchoolName()+"或者->"+schoolName+";name1="+classConfig.getStudents().get(0).getName()
+";name2="+classConfig.getStudents().get(1).getName();
}
}
訪問:http://localhost:8080/index2時得到:“方式二:schoolName=china school或者->china school;name1=tom;name2=jack”
方式三:使用Environment方式
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
public class WebController {
@Autowired
private Environment env;
@RequestMapping("/index3")
public String index3(){
return "方式三:"+env.getProperty("test.msg");
}
}
注意:這種方式是依賴注入Evnironment來完成,在建立的成員變數private Environment env上加上@Autowired註解即可完成依賴注入,然後使用env.getProperty(“鍵名”)即可讀取出對應的值。
訪問:http://localhost:8080/index3時得到:“方式三:Hello World SpringBoot”
二、xiao屬性注入到配置類中不同的兩種寫法
寫法一:
import org.springframework.stereotype.Component;
@Component
public class Configa {
@Value("${name}")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
寫法二:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties //和@ConfigurationProperties(prefix="")等效,不可省略否則會找不到為null
//@PropertySource(value = { "" }) //可省略不寫,預設讀取application.properties
public class Configa {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
三.讀取自定義配置檔案資訊
為了不破壞核心檔案的原生態,但又需要有自定義的配置資訊存在,一般情況下會選擇自定義配置檔案來放這些自定義資訊,這裡在resources目錄下建立配置檔案author.properties。resources/author.properties內容如下:
author.name=Solin
author.age=22
建立管理配置的實體類:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Component
//@ConfigurationProperties(prefix = "author",locations = "classpath:author.properties")
@ConfigurationProperties(prefix = "author")
@PropertySource("classpath:/author.properties")
//多配置檔案引用,若取兩個配置檔案中有相同屬性名的值,則取值為最後一個配置檔案中的值
//@PropertySource({"classpath:/my.properties","classpath:/author.properties"})
public class MyWebConfig{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
注意:
springboot 1.5版本以前@ConfigurationProperties註釋中有兩個屬性:
locations:指定配置檔案的所在位置
prefix:指定配置檔案中鍵名稱的字首(我這裡配置檔案中所有鍵名都是以author.開頭)
springboot 1.5版本以後@ConfigurationProperties沒有了location屬性,使用@PropertySource來指定配置檔案位置
建立測試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
public class ConfigController {
@Autowired
private MyWebConfig conf;
@RequestMapping("/test")
public @ResponseBody String test() {
return "Name:"+conf.getName()+"---"+"Age:"+conf.getAge();
}
}
四.分環境配置
在src/main/resources下新增類似這樣的檔案application-xxx.properties,然後在application.propertie中新增spring.profiles.active=xxx配置則說明首先讀取的是application-xxx.properties而不是application.properties,當使用@Value時則先去找application-xxx.properties,如果application-xxx.properties沒有則再找application.properties,如果兩個檔案都沒有則會報錯Could not resolve placeholder ‘com.blog.test1’ in value “${com.blog.test1}”
五.誤區
1.認為分環境配置可以配置多個
我實踐後發現只能配一個,如在applicatio.properties中這樣配置:
spring.profiles.active=pro
spring.profiles.active=dev
你可能會認為先找application-pro.properties檔案,再找application-dev.properties,最後找application.properties。但實際上是applicatio.properties中最下面的生效,即先找application-dev.properties檔案,再找application.properties檔案,沒有則會報錯
2.兩個配置檔案即內容如下:
application.properties name=hehe
hui.properties name=haha
配置類寫法:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties
@PropertySource("classpath:/hui.properties")
public class Configa {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Controller寫法:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hui.config.Configa;
@RestController
public class WebController {
@Autowired
private Configa configa;
@RequestMapping("/index")
public String index1(){
return configa.getName();
}
}
問:最後返回的值是什麼,是hehe還是haha?
答:最後返回的是hehe,當application.properties和其他配置檔案有相同屬性名則application.properties優先順序大