25. Profiles

Spring Profiles提供了一种方法来隔离应用部分配置,使其只能在特定环境中可用.任何@Component@Configuration标注的实例可以标注@Profile来限制特定的配置文件装载,如以下示例所示:

  1. @Configuration
  2. @Profile("production")
  3. public class ProductionConfiguration {
  4. // ...
  5. }

您可以使用spring.profiles.active环境属性来指定那个配置文件被激活.您可以在本章前面描述的任何方式中指定属性.例如,您可以在application.properties中配置,如下例所示:

  1. spring.profiles.active=dev,hsqldb

您还可以在命令行参数中指定,如下所示:--spring.profiles.active=dev,hsqldb.