Spring-profile 不同环境配置方法


profile

在标签属性中长这个样子

 

配置了当前<beans>标签下 用的是哪个环境(举个例子,当前标签下可能有数据源等配置,可能就会区分测试,线上等不同环境,用的username 和 password等属性就会不同)

Spring 在启动的过程中,会去寻找 “spring.profiles.active” 的属性值,根据这个属性值来决定Bean使用的环境

 

@Profile 表明当前类在那个环境下才会加载

那么配置完profile = "xxx" 后 去哪找这个xxx呢?

Spring 会在这几个地方寻找 spring.profiles.active 的属性值:操作系统环境变量、JVM 系统变量、web.xml 中定义的参数、JNDI。

 

1. 操作系统设置profile

export spring_profiles_active=dev java -jar application.jar 

2.通过Java程序启动JVM参数对profile设定,

java -jar application.jar -Dspring.profiles.active=dev

3.在web.xml中通过context-param元素设置profile。

<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/app-config.xml</param-value> </context-param> <context-param> <param-name>spring.profiles.active</param-name> <param-value>dev</param-value> </context-param>

4. SpringBoot 直接通过application.properties设置profile(推荐)

spring.profiles.active=dev

spring默认加载application.properties这个文件时,发现profile=dev 会同时加载application-dev.properties文件,这样不同的环境根据不同的配置文件做到了环境隔离

 

 获取当前spring容器所使用的环境 String[]

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM