- 根據profile劃分組,用于配置開發環境,測試環境,生産環境
- 在spring中多環境配置需滿足application-{profile}.properties格式,比如application-dev.properties:開發環境
- springboot可以通過在配置檔案中設定spring.profiles.active和spring.profiles.default屬性來設定對應的生效環境,是以可以同時生效多個環境
- spring 隻要在applicationContext.xml 中添加在不同環境下加載的配置檔案即可,例如:
<!-- 開發環境配置檔案 -->
<beans profile="dev">
<context:property-placeholder location="classpath:application-dev.properties" />
</beans>
- 需要注意:profile的定義記得放在文檔的最下邊
-
spring提供許多激活方式:
1. 系統環境變量的方式:通過配置系統環境變量激活profile,spring.profile.default=dev或者
2. JVM參數方式:在容器添加JAVA_OPS,如:ConfigurableEnvironment.setActiveProfiles("test")
3. web.xml的方式:set JAVA_OPTS="-Dspring.profiles.active=dev"
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
4. 注釋方式:</init-param>
5. 以上順序關系激活的優先級别,頂上級别最高@ActiveProfiles({"unittest","devprofile"})