本來地址:Spring Boot乾貨系列:(二)配置文件解析
博客地址:tengj.top/javascript
上一篇介紹了Spring Boot的入門,知道了Spring Boot使用「習慣優於配置」(項目中存在大量的配置,此外還內置了一個習慣性的配置,讓你無需手動進行配置)的理念讓你的項目快速運行起來。因此,咱們要想把Spring Boot玩的溜,就要懂得如何開啓各個功能模塊的默認配置,這就須要瞭解Spring Boot的配置文件application.properties。java
Spring Boot使用了一個全局的配置文件application.properties,放在src/main/resources目錄下或者類路徑的/config下。Sping Boot的全局配置文件的做用是對一些默認配置的配置值進行修改。git
接下來,讓咱們一塊兒來解開配置文件的面紗。github
注:若是你工程沒有這個application.properties,那就在src/main/java/resources目錄下新建一個。spring
application.properties提供自定義屬性的支持,這樣咱們就能夠把一些常量配置在這裏:數據庫
com.dudu.name="嘟嘟MD"
com.dudu.want="祝你們雞年大吉吧"複製代碼
而後直接在要使用的地方經過註解@Value(value="${config.name}")就能夠綁定到你想要的屬性上面tomcat
@RestController
public class UserController {
@Value("${com.dudu.name}")
private String name;
@Value("${com.dudu.want}")
private String want;
@RequestMapping("/")
public String hexo(){
return name+","+want;
}
}複製代碼
咱們啓動工程輸入http://localhost:8080 就能夠看到打印了"嘟嘟MD祝你們雞年大吉吧"。springboot
有時候屬性太多了,一個個綁定到屬性字段上太累,官方提倡綁定一個對象的bean,這裏咱們建一個ConfigBean.java類,頂部須要使用註解@ConfigurationProperties(prefix = "com.dudu")來指明使用哪一個微信
@ConfigurationProperties(prefix = "com.dudu")
public class ConfigBean {
private String name;
private String want;
// 省略getter和setter
}複製代碼
這裏配置完還須要在spring Boot入口類加上@EnableConfigurationProperties並指明要加載哪一個bean,若是不寫ConfigBean.class,在bean類那邊添加hexo
@SpringBootApplication
@EnableConfigurationProperties({ConfigBean.class})
public class Chapter2Application {
public static void main(String[] args) {
SpringApplication.run(Chapter2Application.class, args);
}
}複製代碼
最後在Controller中引入ConfigBean使用便可,以下:
@RestController
public class UserController {
@Autowired
ConfigBean configBean;
@RequestMapping("/")
public String hexo(){
return configBean.getName()+configBean.getWant();
}
}複製代碼
在application.properties中的各個參數之間也能夠直接引用來使用,就像下面的設置:
com.dudu.name="嘟嘟MD"
com.dudu.want="祝你們雞年大吉吧"
com.dudu.yearhope=${com.dudu.name}在此${com.dudu.want}複製代碼
這樣咱們就能夠只是用yearhope這個屬性就好
有時候咱們不但願把全部配置都放在application.properties裏面,這時候咱們能夠另外定義一個,這裏我明取名爲test.properties,路徑跟也放在src/main/resources下面。
com.md.name="喲西~"
com.md.want="祝你們雞年,大吉吧"複製代碼
咱們新建一個bean類,以下:
@Configuration
@ConfigurationProperties(prefix = "com.md")
@PropertySource("classpath:test.properties")
public class ConfigTestBean {
private String name;
private String want;
// 省略getter和setter
}複製代碼
這裏要注意哦,有一個問題,若是你使用的是1.5之前的版本,那麼能夠經過locations指定properties文件的位置,這樣:
@ConfigurationProperties(prefix = "config2",locations="classpath:test.properties")複製代碼
可是1.5版本後就沒有這個屬性了,找了半天發現添加@Configuration和@PropertySource("classpath:test.properties")後才能夠讀取。
配置文件中${random} 能夠用來生成各類不一樣類型的隨機值,從而簡化了代碼生成的麻煩,例如 生成 int 值、long 值或者 string 字符串。
dudu.secret=${random.value}
dudu.number=${random.int}
dudu.bignumber=${random.long}
dudu.uuid=${random.uuid}
dudu.number.less.than.ten=${random.int(10)}
dudu.number.in.range=${random.int[1024,65536]}複製代碼
Spring Boot是基於jar包運行的,打成jar包的程序能夠直接經過下面命令運行:
java -jar xx.jar複製代碼
能夠如下命令修改tomcat端口號:
java -jar xx.jar --server.port=9090複製代碼
能夠看出,命令行中連續的兩個減號--
就是對application.properties
中的屬性值進行賦值的標識。
因此java -jar xx.jar --server.port=9090
等價於在application.properties
中添加屬性server.port=9090
。
若是你怕命令行有風險,可使用SpringApplication.setAddCommandLineProperties(false)禁用它。
實際上,Spring Boot應用程序有多種設置途徑,Spring Boot能從多重屬性源得到屬性,包括以下幾種:
~/.spring-boot-devtools.properties
)。SPRING_APPLICATION_JSON
中的屬性(環境變量或系統屬性中的內聯JSON嵌入)。ServletConfig
初始化參數。ServletContext
初始化參數。SpringApplication.setDefaultProperties
指定).這裏列表按組優先級排序,也就是說,任何在高優先級屬性源裏設置的屬性都會覆蓋低優先級的相同屬性,列如咱們上面提到的命令行屬性就覆蓋了application.properties的屬性。
application.properties和application.yml文件能夠放在一下四個位置:
一樣,這個列表按照優先級排序,也就是說,src/main/resources/config下application.properties覆蓋src/main/resources下application.properties中相同的屬性,如圖:
此外,若是你在相同優先級位置同時有application.properties和application.yml,那麼application.yml裏面的屬性就會覆蓋application.properties裏的屬性。
當應用程序須要部署到不一樣運行環境時,一些配置細節一般會有所不一樣,最簡單的好比日誌,生產日誌會將日誌級別設置爲WARN或更高級別,並將日誌寫入日誌文件,而開發的時候須要日誌級別爲DEBUG,日誌輸出到控制檯便可。
若是按照之前的作法,就是每次發佈的時候替換掉配置文件,這樣太麻煩了,Spring Boot的Profile就給咱們提供瞭解決方案,命令帶上參數就搞定。
這裏咱們來模擬一下,只是簡單的修改端口來測試
在Spring Boot中多環境配置文件名須要知足application-{profile}.properties
的格式,其中{profile}
對應你的環境標識,好比:
想要使用對應的環境,只須要在application.properties中使用spring.profiles.active屬性來設置,值對應上面提到的{profile},這裏就是指dev、prod這2個。
固然你也能夠用命令行啓動的時候帶上參數:
java -jar xxx.jar --spring.profiles.active=dev複製代碼
我給不一樣的環境添加不一樣的端口屬性server.port,而後根據指定不一樣的spring.profiles.active來切換使用。各位能夠本身試試。這裏就不貼代碼了。
除了能夠用profile的配置文件來分區配置咱們的環境變量,在代碼裏,咱們還能夠直接用@Profile註解來進行配置,例如數據庫配置,這裏咱們先定義一個接口
public interface DBConnector { public void configure(); }複製代碼
分別定義倆個實現類來實現它
/** * 測試數據庫 */
@Component
@Profile("testdb")
public class TestDBConnector implements DBConnector {
@Override
public void configure() {
System.out.println("testdb");
}
}
/** * 生產數據庫 */
@Component
@Profile("devdb")
public class DevDBConnector implements DBConnector {
@Override
public void configure() {
System.out.println("devdb");
}
}複製代碼
經過在配置文件激活具體使用哪一個實現類
spring.profiles.active=testdb複製代碼
而後就能夠這麼用了
@RestController
@RequestMapping("/task")
public class TaskController {
@Autowired DBConnector connector ;
@RequestMapping(value = {"/",""})
public String hellTask(){
connector.configure(); //最終打印testdb
return "hello task !! myage is " + myage;
}
}複製代碼
除了spring.profiles.active來激活一個或者多個profile以外,還能夠用spring.profiles.include來疊加profile
spring.profiles.active: testdb
spring.profiles.include: proddb,prodmq複製代碼
此次對Spring Boot中application.properties配置文件作的整理總結但願對你們有所幫助,最後貼上Spring Boot中經常使用的配置屬性,須要的時候可打開查找。
( ̄︶ ̄)↗Spring Boot乾貨系列:經常使用應用程序屬性
想要查看更多Spring Boot乾貨教程,可前往:Spring Boot乾貨系列總綱
Spring Boot屬性配置文件詳解
spring boot 使用profile來分區配置
Spring Boot實戰
JavaEE開發的顛覆者Spring Boot實戰
( ̄︶ ̄)↗[相關示例完整代碼]
一直以爲本身寫的不是技術,而是情懷,一篇篇文章是本身這一路走來的痕跡。靠專業技能的成功是最具可複製性的,但願個人這條路能讓你少走彎路,但願我能幫你抹去知識的蒙塵,但願我能幫你理清知識的脈絡,但願將來技術之巔上有你也有我,但願大爺你看完打賞點零花錢給我。
訂閱博主微信公衆號:嘟爺java超神學堂(javaLearn)三大好處:
我手裏有幾本Spring Boot的中文電子書資料,有須要的能夠關注博主微信公衆號(點擊頭像就能看博主公衆號),自行前往下載【開發工具->java電子書籍】