springboot多环境配置文件、
使用springboot2.1.1-RELEASE脚手架创建web、
多环境配置文件目录结构、
application.yml配置文件如下:
server:
servlet:
context-path: /
spring:
profiles:
active: test #配置文件切换成test 环境
把环境切换到测试环境中、测试端口为8011 、重启main测试;
application-test.yml 如下:
server:
port: 8011
读取配置文件中的信息、配置application-test.yml 、并添加组件config,把组件注入到controller 中,请求读取student 信息;
student: # application-test.yml添加 student 属性
name: test-doudou
age: 15
/**
* @auther SyntacticSugar
* @data 2018/12/12 0012上午 10:59
*/
@Component
public @Data class StuConfig {
@Value("${student.name}")
private String name;
@Value("${student.age}")
private Integer age;
}
controller 中取值、并请求 http://localhost:8011/rest/showNews
@Autowired
private StuConfig stuConfig;
// 读取配置文件中的student属性
@GetMapping("showNews")
public String show(){
return "hello "+stuConfig.getName()+",age :"+stuConfig.getAge();
}