- 根据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或者ConfigurableEnvironment.setActiveProfiles("test")
2. JVM参数方式:在容器添加JAVA_OPS,如:set JAVA_OPTS="-Dspring.profiles.active=dev"
3. web.xml的方式:
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</init-param>
4. 注释方式:@ActiveProfiles({"unittest","devprofile"})
5. 以上顺序关系激活的优先级别,顶上级别最高