相信不少人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷。咱们在Spring Boot使用过程当中,最直观的感觉就是没有了原来本身整合Spring应用时繁多的XML配置内容,替代它的是在pom.xml
中引入模块化的Starter POMs
,其中各个模块都有本身的默认配置,因此若是不是特殊应用场景,就只须要在application.properties
中完成一些属性配置就能开启各模块的应用。java
pom包里面添加相关包引用git
<!-- Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 单元测试依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
在以前的各篇文章中都有说起关于application.properties
的使用,主要用来配置数据库链接、日志相关配置等。除了这些配置内容以外,本文将具体介绍一些在application.properties
配置中的其余特性和使用方法。github
而后经过@Value("${属性名}")
注解来加载对应的配置属性,具体以下:web
@RestController public class UserController { @Value("${user.desc}") private String desc; @RequestMapping(value = "/user",method = RequestMethod.GET) public String getDesc(){ return desc; } }
@RestController public class HelloController { @RequestMapping(value = "/", method = RequestMethod.GET) public String index(){ return "Welcome Spring Boot Properties!!!"; } @Autowired HomeProperties homeProperties; @RequestMapping(value = "/home",method = RequestMethod.GET) public String getDesc(){ return homeProperties.getDesc(); } }
在application.properties
中的各个参数之间也能够直接引用来使用,就像下面的设置:spring
## 家乡属性 Dev home.province=ZheJiang home.city=WenLing home.desc=dev: I'm living in ${home.province} ${home.city}. ###我的属性 user.name=ilimhumar user.age=25 user.job=Java Programmer user.weight = 70kg user.height=180cm user.desc= My name's ${user.name} ${user.age} ${user.job} ${user.weight} ${user.height}
咱们在开发Spring Boot应用时,一般同一套程序会被应用和安装到几个不一样的环境,好比:开发、测试、生产等。其中每一个环境的数据库地址、服务器端口等等配置都会不一样,若是在为不一样环境打包时都要频繁修改配置文件的话,那必将是个很是繁琐且容易发生错误的事。数据库
对于多环境的配置,各类项目构建工具或是框架的基本思路是一致的,经过配置多份不一样环境的配置文件,再经过打包命令指定须要打包的内容以后进行区分打包,Spring Boot也不例外,或者说更加简单。服务器
在Spring Boot中多环境配置文件名须要知足application-{profile}.properties
的格式,其中{profile}
对应你的环境标识,好比:app
application-dev.properties
:开发环境application-test.properties
:测试环境application-prod.properties
:生产环境至于哪一个具体的配置文件会被加载,须要在application.properties
文件中经过spring.profiles.active
属性来设置,其值对应{profile}
值。框架
如:spring.profiles.active=test
就会加载application-test.properties
配置文件内容模块化
下面,以不一样环境配置不一样的服务端口为例,进行样例实验。
针对各环境新建不一样的配置文件application-dev.properties
、application-test.properties
、application-prod.properties
在这三个文件均都设置不一样的server.port
属性,如:dev环境设置为1111,test环境设置为2222,prod环境设置为3333
application.properties中设置spring.profiles.active=dev
,就是说默认以dev环境设置
测试不一样配置的加载
application.properties 配置文件
# 多环境配置文件激活属性 spring.profiles.active=dev
application-dev.properties 配置文件
# 服务端口 server.port=1111 ## 家乡属性 Dev home.province=ZheJiang home.city=WenLing home.desc=dev: I'm living in ${home.province} ${home.city}. ###我的属性 user.name=ilimhumar user.age=25 user.job=Java Programmer user.weight = 70kg user.height=180cm user.desc= My name's ${user.name} ${user.age} ${user.job} ${user.weight} ${user.height}
application-prod.properties 配置文件
# 服务端口 server.port=2222 ## 家乡属性 Prod home.province=guangdong home.city=shenzhen home.desc=prod: I'm living in ${home.province} ${home.city}. ###我的属性 user.name=bilzat user.age=24 user.job=PHP Programmer user.weight = 80kg user.height=180cm user.desc= My name's ${user.name} ${user.age} ${user.job} ${user.weight} ${user.height}
application-test.properties 配置文件
# 服务端口 server.port=3333 ## 家乡属性 Prod home.province=jiangsu home.city=hangzhou home.desc=test: I'm living in ${home.province} ${home.city}. ###我的属性 user.name=elzat user.age=26 user.job=C++ Programmer user.weight = 75kg user.height=180cm user.desc= My name's ${user.name} ${user.age} ${user.job} ${user.weight} ${user.height}
运行效果