本篇博客主要講解下在Spring Boot中如何獲取配置文件的值。
1. 使用yaml配置文件
Spring Boot默認生成的配置文件為application.properties,不過它也支持yaml語言的配置文件,
兩者之間的差別並不是很大,只是yaml語言的配置文件層次結構更明顯,可讀性更強,因此目前使用的更多一些。
我們假設本來application.properties的配置為:
spring.main.banner-mode=off
那么如何將配置文件切換為application.yml呢?
首先刪除掉原來的配置文件application.properties,新增配置文件application.yml,新增方式如下:
然后修改application.yml為:
spring:
main:
banner-mode: "off"
運行結果和原來使用properties配置文件時一致。
2. 使用@Value注解獲取配置文件值
首先在application.yml中添加如下配置:
book:
author: wangyunfei
name: spring boot
然后修改啟動類的代碼為:
package com.zwwhnly.springbootaction;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SpringbootActionApplication {
@Value("${book.name}")
private String bookName;
@Value("${book.author}")
private String bookAuthor;
@RequestMapping("/")
public String index() {
return "book name is:" + bookName + " and book author is:" + bookAuthor;
}
public static void main(String[] args) {
SpringApplication.run(SpringbootActionApplication.class, args);
}
}
運行項目,在瀏覽器中輸入http://localhost:8080/,會看到如下信息:
這種方式和在Spring項目中的使用方式一樣,更多的細節,可以查看我之前總結的博客:
3. 使用@ConfigurationProperties注解獲取配置文件值
Spring Boot還提供了@ConfigurationProperties
注解來獲取配置文件值,該種方式可把配置文件值和一個Bean自動關聯起來,使用起來更加方便而且類型安全,建議使用這種方式。
首先,在application.yml中添加如下配置:
author:
name: wangyunfei
age: 32
然后,新建類AuthorSettings,添加@Component
注解和@ConfigurationProperties
注解:
package com.zwwhnly.springbootaction;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "author")
public class AuthorSettings {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
這里的重點是@ConfigurationProperties
注解,它的prefix屬性用來指定配置的前綴,如本例中的author。
然后修改啟動類的代碼為:
package com.zwwhnly.springbootaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SpringbootActionApplication {
@Autowired
private AuthorSettings authorSettings;
@RequestMapping("/")
public String index() {
return "author name is " + authorSettings.getName() + " and author age is " + authorSettings.getAge();
}
public static void main(String[] args) {
SpringApplication.run(SpringbootActionApplication.class, args);
}
}
運行項目,在瀏覽器中輸入http://localhost:8080/,會看到如下信息:
4. 使用Profile實現多環境配置管理
一般情況下,我們開發的應用程序都會有多套環境, 如dev環境,qa環境,prod環境,那么如何實現多套環境下的配置管理呢?
其實在Spring Boot下,我們可以使用Profile來實現,接下來講解下具體的實現方式。
首先,新建2個配置文件:application-dev.yml,application-prod.yml。
此時的項目結構如下圖所示:
如果有的同學比較喜歡用properties文件,可以用下圖中的方式新建:
默認情況下,啟動的端口號為8080,如果我們希望在dev環境使用端口號8082,在prod環境使用端口號8083,那么可以修改配置文件如下:
application-dev.yml新增如下配置:
server:
port: 8082
application-prod.yml新增如下配置:
server:
port: 8083
運行項目,如下圖所示:
我們會發現,仍然使用的是默認的端口號8080,那么如何指定使用dev或者prod環境的端口呢?
我們需要在application.yml新增如下配置:
spring:
profiles:
active: dev
此時,再次運行項目,會發現使用的是端口號8082,也就是application-dev.yml文件中配置的。
如果希望使用prod環境的,可以修改配置為:
spring:
profiles:
active: prod
運行結果為:
關於Spring項目中Profile的使用可以查看我之前總結的博客:Spring入門(七):Spring Profile使用講解。
5. 源碼及參考
源碼地址:https://github.com/zwwhnly/springboot-action.git,歡迎下載。
汪雲飛《Java EE開發的顛覆者:Spring Boot實戰》