25. Profiles
Spring Profiles提供了一种隔离应用程序配置的方式,并让这些配置只在特定的环境下生效。任何@Component
或@Configuration
都能被@Profile
标记,从而限制加载它的时机:
@Configuration
@Profile("production")
public class ProductionConfiguration {
// ...
}
按照Spring最常用的方式,你可以使用spring.profiles.active
这个Environment属性来指定哪个配置生效。你可以使用任何设置Environment属性方式来指定该属性,例如,可以将它包含到application.properties
中:
spring.profiles.active=dev,hsqldb
或者使用命令行“开关”--spring.profiles.active=dev,hsqldb
(Spring Boot应用的program arguments)