在上一篇博客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,欢迎你们下载,有问题能够多多交流。