SpringBoot中spring.profiles.active配置实现多环境区分
spring boot允许通过命名约定按照一定的格式application-{profile}.properties来定义多个配置文件,然后通过在application.properties的spring.profiles.active来具体激活一个或者多个配置文件,如果没有没有指定任何profile的配置文件的话,spring boot默认会启动application-default.properties。
在项目开发中有多个配置文件
application-dev.yml //开发环境 application-test.yml //测试环境 application-pro.yml //生产环境
1、只需要在源配置文件application.yaml上面增加下面的一个配置,就代表指定使用哪一个配置文件:
spring:
profiles:
active: dev #当前使用application-dev.yml配置
2、也可以在java启动参数中指定,动态选择运行环境
执行java -jar xxx.jar --spring.profiles.active=test,运行测试环境的配置
执行java -jar xxx.jar --spring.profiles.active=prod,运行生产环境的配置
3、也可以动态选择运行环境, 当没有指定环境的时候, 默认选择dev环境

spring:
profiles:
active: ${profile:dev} #当前使用application-dev.yml配置
4、在项目代码中使用@Profile区分环境,使某些代码在指定环境下执行
下面类只在dev环境下执行,其他环境不会生效
@Component
@Slf4j
@Profile("dev") //生产环境执行.
public class InitOtherService implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
// todo
}
}
到此这篇关于SpringBoot中spring.profiles.active配置实现多环境区分的文章就介绍到这了,更多相关SpringBoot 多环境区分内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Springboot实现WebMvcConfigurer接口定制mvc配置详解
这篇文章主要介绍了Springboot实现WebMvcConfigurer接口定制mvc配置详解,spring boot抛弃了传统xml配置文件,通过配置类(标注@Configuration的类,@Configuration配置类相当于一个xml配置文件)以JavaBean形式进行相关配置,需要的朋友可以参考下2023-09-09
详解Spring MVC如何测试Controller(使用springmvc mock测试)
这篇文章主要介绍了详解Spring MVC如何测试Controller(使用springmvc mock测试),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-12-12


最新评论