2小时学会Spring Boot学习-项目属性配置

1.项目属性配置

resources文件夹下新建 application.yml application-dev.yml application-prod.yml文件

application.yml按需读取dev或prod中的参数

application.yml文件

spring:
profiles:
active: dev

application-dev.yml文件
server:
port: 8080
girl:
cupSize: B
age: 18

application-prod.yml文件
server:
port: 8081
girl:
cupSize: F
age: 18

新建GirlProperties.java

@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {

private String cupSize;

private Integer age;


public String getCupSize() {
return cupSize;
}

public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

}

controller类中调用
@RestController
public class HelloController {

@Autowired
private GirlProperties girlProperties;

@GetMapping(value = "/hello")
public String say(){
return girlProperties.getCupSize();
}
}
 

 

posted @ 2018-04-20 15:25  zhcnblog  阅读(151)  评论(0编辑  收藏  举报