赞
踩
通过在application.properties中配置下面的属性来实现
# PROFILES
spring.profiles.active= # Comma-separated list of active profiles. Can be overridden by a command line switch.
## 家乡属性 Dev
home.province=广东-dev
home.city=深圳-dev
home.desc=我生活在 ${home.province} ${home.city}.
## 家乡属性 Dev
home.province=广东-tet
home.city=深圳-test
home.desc=我生活在 ${home.province} ${home.city}.
## 家乡属性 Dev
home.province=广东-prep
home.city=深圳-prep
home.desc=我生活在 ${home.province} ${home.city}.
## 家乡属性 Dev
home.province=广东-prod
home.city=深圳-prod
home.desc=我生活在 ${home.province} ${home.city}.
#profiles
spring.profiles.active=dev
package com.ghgcn.hello.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "home") public class HomeProperties { private String province; private String city; private String desc; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @Override public String toString() { return "HomeProperties{" + "province='" + province + '\'' + ", city='" + city + '\'' + ", desc='" + desc + '\'' + '}'; } }
@RestController
public class HelloController {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private HomeProperties homeProperties;
@GetMapping("/hello/home")
public HomeProperties getHome(){
logger.info(homeProperties.toString());
return homeProperties;
}
}
切换为test
只需要在application.properties中替换变量即可
#profiles
spring.profiles.active=${变量}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。