Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只在特定的环境下生效。任何 @Component 或 @Configuration 都能注解 @Profile ,从而限制加载它的时机:
@Configuration
@Profile("prod")
public class ProductionConfiguration {
// ...
}
可以在application.properties中配置
spring.profiles.active=dev,hsqldb
或使用命令行开关:
--spring.profiles.active=dev,hsqldb
还可以以编程方式设置profiles。
在应用运行前,你可以通过调用 SpringApplication.setAdditionalProfiles(…) 方法,以编程的方式设置激活的配置,也可以使用Spring的 ConfigurableEnvironment 接口激活配置(profiles)。