SpringBoot 是爲了簡化 Spring 應用的創建、運行、調試、部署等一系列問題而誕生的產物,自動裝配的特性讓我們可以更好的關注業務本身而不是外部的XML配置,我們只需遵循規範,引入相關的依賴就可以輕易的搭建出一個 WEB 工程
上一篇 介紹了 SpringBoot 由來及構建方式,通過第一章的教程我們對 SpringBoot
不在感到陌生,可以發現 SpringBoot
雖然幹掉了 XML 但未做到 零配置,它體現出了一種 約定優於配置,也稱作按約定編程,是一種軟件設計範式,旨在減少軟件開發人員需做決定的數量,獲得簡單的好處,而又不失靈活性。 一般情況下默認的配置足夠滿足日常開發所需,但在特殊的情況下,我們往往需要用到自定義屬性配置、自定義文件配置、多環境配置、外部命令引導等一系列功能。不用擔心,這些 SpringBoot 都替我們考慮好了,我們只需要遵循它的規則配置即可
準備前提
爲了讓 SpringBoot 更好的生成數據,我們需要添加如下依賴(該依賴可以不添加,但是在 IDEA 和 STS 中不會有屬性提示,沒有提示的配置就跟你用記事本寫代碼一樣苦逼,出個問題弄哭你去),該依賴只會在編譯時調用,所以不用擔心會對生產造成影響
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
自定義屬性配置
在 application.properties 寫入如下配置內容
my1.age=22
my1.name=battcn
其次定義 MyProperties1.java 文件,用來映射我們在 application.properties 中的內容,這樣一來我們就可以通過操作對象的方式來獲得配置文件的內容了
package com.battcn.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author Levin
* @since 2018/4/23 0023
*/
@Component
@ConfigurationProperties(prefix = "my1")
public class MyProperties1 {
private int age;
private String name;
// 省略 get set
@Override
public String toString() {
return "MyProperties1{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
接下來就是定義我們的 PropertiesController 用來注入 MyProperties1 測試我們編寫的代碼,值得注意的是 Spring4.x 以後,推薦使用構造函數的形式注入屬性…
import com.battcn.properties.MyProperties1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Levin
* @since 2018/4/23 0023
*/
@RequestMapping("/properties")
@RestController
public class PropertiesController {
private static final Logger log = LoggerFactory.getLogger(PropertiesController.class);
private final MyProperties1 myProperties1;
@Autowired
public PropertiesController(MyProperties1 myProperties1) {
this.myProperties1 = myProperties1;
}
@GetMapping("/1")
public MyProperties1 myProperties1() {
log.info("=================================================================================================");
log.info(myProperties1.toString());
log.info("=================================================================================================");
return myProperties1;
}
}
打開瀏覽器,輸入如下地址: http://localhost:8080/properties/1,觀察控制檯,監聽到如下內容則表示程序正確
2020-03-08 15:51:43.145 INFO 15352 --- [nio-8080-exec-2] c.b.controller.PropertiesController : =================================================================================================
2020-03-08 15:51:43.145 INFO 15352 --- [nio-8080-exec-2] c.b.controller.PropertiesController : MyProperties1{age=22, name='battcn'}
2020-03-08 15:51:43.145 INFO 15352 --- [nio-8080-exec-2] c.b.controller.PropertiesController : =================================================================================================
自定義 配置文件
定義一個名爲 my2.properties 的資源文件,自定義配置文件的命名不強制 application 開頭
my2.age=22
my2.name=Levin
[email protected]
其次定義MyProperties2.java
文件,用來映射我們在 my2.properties
中的內容。
package com.battcn.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:my2.properties")
@ConfigurationProperties(prefix = "my2")
public class MyProperties2 {
private int age;
private String name;
private String email;
// 省略 get set
@Override
public String toString() {
return "MyProperties2{" +
"age=" + age +
", name='" + name + '\'' +
", email='" + email + '\'' +
'}';
}
}
接下來在PropertiesController
用來注入MyProperties2
測試我們編寫的代碼
@GetMapping("/2")
public MyProperties2 myProperties2() {
log.info("=================================================================================================");
log.info(myProperties2.toString());
log.info("=================================================================================================");
return myProperties2;
}
打開瀏覽器,輸入如下地址: http://localhost:8080/properties/2,觀察控制檯,監聽到如下內容則表示程序正確
2020-03-08 15:59:45.395 INFO 6232 --- [nio-8080-exec-4] c.b.controller.PropertiesController : =================================================================================================
2020-03-08 15:59:45.395 INFO 6232 --- [nio-8080-exec-4] c.b.controller.PropertiesController : MyProperties2{age=22, name='Levin', email='[email protected]'}
2020-03-08 15:59:45.395 INFO 6232 --- [nio-8080-exec-4] c.b.controller.PropertiesController : =================================================================================================
多環境化配置
在真實的應用中,常常會有多個環境(如:開發,測試,生產等)
,不同的環境數據庫連接都不一樣,這個時候就需要用到spring.profile.active 的強大功能了.
它的格式爲 application-{profile}.properties,這裏的 application 爲前綴不能改,{profile} 是我們自己定義的。
創建
application-dev.properties
、application-test.properties
、application-prod.properties
,內容分別如下
application-dev.properties :
server.servlet.context-path=/dev
application-test.properties
server.servlet.context-path=/test
application-prod.properties
server.servlet.context-path=/prod
在 application.properties
配置文件中寫入 spring.profiles.active=dev
,這個時候我們在次訪問 http://localhost:8080/properties/1 就沒用處了,因爲我們設置了它的context-path=/dev,所以新的路徑就是 http://localhost:8080/dev/properties/1 ,由此可以看出來我們激活不同的配置讀取的屬性值是不一樣的
外部命令引導
前面三種方式都是基於配置文件層面的,那麼有沒有辦法外部引導呢,假設這樣的場景,我們對已經開發完成的代碼打包發佈,期間在測試環境測試通過了,那麼即可發佈上生產,這個時候是修改application.properties的配置方便還是直接在命令參數配置方便呢,毫無疑問是後者更有說服力。默認情況下,SpringApplication 會將命令行選項參數(即:–property,如–server.port=9000)添加到Environment,命令行屬性始終優先於其他屬性源。
如何測試?
- 進入到項目目錄,此處以我本地目錄爲主:
F:/battcn-workspace/spring-boot2-learning/chapter2
- 然後打開 cmd 程序,不會在當前目錄打開 cmd 的請自行百度,輸入:mvn package
- 打包完畢後進入到:F:/battcn-workspace/spring-boot2-learning/chapter2/target 目錄中去,我們可以發現一個名爲chapter2-0.0.1-SNAPSHOT.jar 的包
- 接着在打開 cmd 程序,
- 輸入:
java -jar chapter2-0.0.1-SNAPSHOT.jar --spring.profiles.active=test --my1.age=32
。 - 仔細觀察
spring.profiles.active=test、my1.age=32
這倆配置的鍵值是不是似曾相識(不認識的請從開頭認真閱讀) - 最後輸入測試地址:http://localhost:8080/test/properties/1 我們可以發現返回的JSON變成了 {“age”:32,“name”:“battcn”} 表示正確
總結
- 掌握
@ConfigurationProperties
、@PropertySource
等註解的用法及作用 - 掌握編寫自定義配置
- 掌握外部命令引導配置的方式
目前很多大佬都寫過關於 SpringBoot 的教程了,如有雷同,請多多包涵,本教程基於的 spring-boot-starter-parent:2.0.1.RELEASE
編寫