一,多环境配置
在项目开发声明周期中,可能需要经历开发环境 --> 测试环境 --> 预生产环境 --> 生产环境几个阶段,在每个阶段中都需要对应不同环境的配置参数,比如数据库连接。因此在配置文件配置时,应该针对不同环境配置不同的参数,并通过引用不同的配置信息适配不同的环境
二,多环境配置文件
* 开发环境 application-dev.properties
## 开发环境
http_url=dev.springboot.com
* 测试环境 application-test.properties
## 测试环境
http_url=test.springboot.com
* 生产环境 application-prd.properties
## 生产环境
http_url=prd.springboot.com
三,主配置文件引入不同的配置信息;配置文件命名方式为application-*.properties,主配置文件引入配置信息时,只需要引入*部分名称即可
### 此处表示引入测试环境配置文件
spring.profiles.active=test
四,通过@Value注解注入配置信息到成员变量,并展示
package com.gupao.springboot.test.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 多环境配置
* @author pj_zhang
* @create 2018-12-25 22:03
**/
@RestController
public class ParamController {
@Value("${http_url}")
private String httpUrl;
@RequestMapping("/param")
public String param() {
return httpUrl;
}
}
五,测试结果