springBoot不同环境读取不同的配置
springBoot不同环境读取不同的配置
在application.properties 中指定使用 哪个环境的配置文件:
spring.profiles.active=dev
当 spring.profiles.active 为 dev 时,会读取 application-dev.properties 的配置。
当 spring.profiles.active 为 test 时,会读取 application-test.properties 的配置。
新建 application-dev.properties 文件,配置值如下:
my.value=1
新建 application-test.properties 文件,配置如下:
my.value=2
读取配置的值
@Service
public class ProfileServiceImpl {
/**
* :冒号后面,表示默认值.
*/
@Value("${my.value:0}")
private int myValue;
public int getMyValue() {
return myValue;
}
}
测试类:
@Test
public void testGetMyValue() throws Exception {
//TODO: Test goes here...
int myValue = profileService.getMyValue();
System.out.println(myValue);
Assert.assertNotEquals(myValue, 0);
}
执行测试类,发现读取到了 dev环境的 变量,结果为1。
修改 spring.profiles.active 的配置值为 test, 再次执行,读取到了 test 环境的 变量,结果为 2.
maven 配置profile指定环境
环境 spring.profiles.active 的值,可以通过 maven 来配置。
详情见: https://www.cnblogs.com/expiator/p/17544134.html
浙公网安备 33010602011771号