Spring Profiles 允許配置可用於特定環境的部件
任何 @Component
或 @Configuration
都可以用 @Profile
註釋標記
@Configuration
@Profile("production")
public class ProductionConfiguration {
// ...
}
在 XML 配置中也是如此
<beans profile="dev">
<bean id="dataSource" class="<some data source class>" />
</beans>
可以在 application.properties
檔案中配置活動配置檔案
spring.profiles.active=dev,production
或從命令列指定
--spring.profiles.active=dev,hsqldb
或者在 SpringBoot 中
SpringApplication.setAdditionalProfiles("dev");
可以使用註釋 @ActiveProfiles("dev")
在測試中啟用配置檔案