在上一篇博客Spring Boot入門(一):使用IDEA建立Spring Boot項目並使用yaml配置文件中,咱們新建了一個最原始的Spring Boot項目,並使用了更爲流行的yaml配置文件。html
可是通常狀況下,咱們開發的系統應用都會有多套環境, 如dev環境,qa環境,prod環境,java
那麼如何實現多套環境下的配置管理呢?git
其實在Spring Boot下,咱們可使用Profile來實現,如下來說解具體的實現方式。github
首先咱們按照上篇博客中提到的方法新建2個配置文件:application-dev.yml,application-prod.yml。web
若是有的同窗比較喜歡用properties文件,能夠用下圖中的方式新建:spring
咱們知道,默認狀況下,啓動的端口號爲8080,若是咱們但願在dev環境使用端口號8082,在prod環境使用端口號8083,那麼能夠修改配置文件以下:瀏覽器
application-dev.yml新增以下配置:springboot
server: port: 8082
application-prod.yml新增以下配置:app
server: port: 8083
此時,啓動下Spring Boot項目this
咱們會發現,仍然使用的是默認的端口號8080,那麼如何指定使用dev或者prod環境的端口呢?
咱們須要在application.yml新增以下配置:
spring: profiles: active: dev
此時,再次啓動Spring Boot項目,會發現使用的是端口號8082,也就是application-dev.yml文件中配置的。
若是但願使用prod環境的,能夠修改配置爲:
spring: profiles: active: prod
運行結果爲:
既然用到了配置文件,並且在平時的開發過程當中,常常會將一些可能會修改的值放到配置文件中,那麼在Spring Boot中,如何獲取配置的文件的值呢?
咱們如今就講解下使用@Value註解或者@ConfigurationProperties註解來獲取配置文件值的方法。
首先在application.yml中添加以下配置:
book: author: wangyunfei name: spring boot
而後修改Spring Boot項目啓動類的代碼以下:
package com.zwwhnly.springbootdemo; 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 SpringbootdemoApplication { @Value("${book.author}") private String bookAuthor; @Value("${book.name}") private String bookName; @RequestMapping("/") String index() { return "book name is:" + bookName + " and book author is:" + bookAuthor; } public static void main(String[] args) { SpringApplication.run(SpringbootdemoApplication.class, args); } }
啓動項目,在瀏覽器輸入http://localhost:8080/,會看到以下信息:
在方式1中,咱們使用@Value註解來獲取配置文件值,但若是多個地方都須要獲取的話,就須要在多個地方寫註解,形成混亂,很差管理,
其實,Spring Boot還提供了@ConfigurationProperties註解來獲取配置文件值,該種方式可把配置文件值和一個Bean自動關聯起來,使用起來更加方便,我的建議用這種方式。
在application.yml中添加以下配置:
author: name: wangyunfei age: 32
新建類AuthorSettings,添加@Component和@ConfigurationProperties註解
package com.zwwhnly.springbootdemo; 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; } }
而後修改啓動類代碼以下:
package com.zwwhnly.springbootdemo; import org.springframework.beans.factory.annotation.Autowired; 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 SpringbootdemoApplication { @Value("${book.author}") private String bookAuthor; @Value("${book.name}") private String bookName; @Autowired private AuthorSettings authorSettings; @RequestMapping public String hello() { return "Hello Spring Boot!"; } @RequestMapping("/") public String index() { return "book name is:" + bookName + " and book author is:" + bookAuthor; } @RequestMapping("/indexV2") public String indexV2() { return "author name is:" + authorSettings.getName() + " and author age is:" + authorSettings.getAge(); } public static void main(String[] args) { SpringApplication.run(SpringbootdemoApplication.class, args); } }
啓動項目,在瀏覽器輸入http://localhost:8080/indexV2,會看到以下信息:
https://github.com/zwwhnly/springbootdemo.git,歡迎你們下載,有問題能夠多多交流。