spring boot配置多环境资源文件
项目结构
文件格式
#{profile}自定义的不同环境标识
application-{profile}.properties
测试
文件内容
#application-dev.properties
com.sxd.ip = 127.0.0.1
com.sxd.value = 开发环境
#application-test.properties
com.sxd.ip = 192.168.15.14
com.sxd.value = 测试环境
#application-pro.properties
com.sxd.ip = 102.118.174.14
com.sxd.value = 生产环境
#application.properties
#选择开发环境资源文件,可多选
spring.profiles.active=dev
#spring.profiles.include=dev,test,pro
绑定使用
package com.sxd.beans;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "com.sxd")
public class ConfigBean {
private String ip;
private String value;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
运行校验
package com.sxd.firstdemo; import com.sxd.beans.ConfigBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication @EnableConfigurationProperties({ConfigBean.class}) public class FirstdemoApplication { @Autowired ConfigBean configBean; @RequestMapping("/") public String index(){ return "IP:"+configBean.getIp()+"\n环境:"+configBean.getValue(); } public static void main(String[] args) { SpringApplication.run(FirstdemoApplication.class, args); } }
不曾提起,却从未忘记