版权声明:本文为博主原创文章,未经博主允许不得转载。 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