SpringBoot使用profile配置不同环境配置生效

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/buyaore_wo/article/details/78092168

Spring Boot使用@Profile注解可以实现不同环境下配置参数的切换,任何@Component@Configuration注解的类都可以使用@Profile注解。

例如:

@Configuration
@Profile("production")
public class ProductionConfiguration {
    // ...
}

通常,一个项目中可能会有多个profile场景,例如开发场景:

@Configuration
@Profile("dev")
public class TestConfiguration {
    // ...
}

在存在多个profile情况下,你可以使用spring.profiles.active来设置哪些profile被激活。spring.profiles.include属性用来设置无条件的激活哪些profile。

例如,你可以在application.properties中设置:

spring.profiles.active=dev,hsqldb

或者在application.yaml中设置:

spring.profiles.active:dev,hsqldb

spring.profiles.active属性可以通过命令行参数: --spring.profiles.active=dev,hsqldb.


添加三个配置文件

application-dev.properties

application-production.properties

application-hsqldb.properties


通过application.properties中的

spring.profiles.active=dev,hsqldb
来配置激活哪个配置,或命令行参数

--spring.profiles.active=dev,hsqldb.


官方链接: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

猜你喜欢

转载自blog.csdn.net/buyaore_wo/article/details/78092168