【Java Web开发学习】Spring环境profile
【Java Web开发学习】Spring 环境profile
转载:http://www.cnblogs.com/yangchongxing/p/8890702.html
开发、测试、生产环境往往是不同的,我们需要将应用从一个环境迁移到另外一个环境,这时候就牵扯到不同环境配置是不同的。
Spring提供了@Profile注解来指定bean属于哪一个profile
1、配置@Profile注解,该注解可用于类也可用于方法
用于类上只有当该profile是激活状态时,这个类的bean才能被创建
package cn.ycx.web.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import cn.ycx.web.model.Painter; import cn.ycx.web.model.Player; @Configuration @Profile("dev") public class TestConfig { @Bean public Player player() { return new Player(); } @Bean public Painter painter() { return new Painter(); } }
用于方法上只有当该profile是激活状态时,标注@Bean的方法才能被创建bean
package cn.ycx.web.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import cn.ycx.web.model.Painter; import cn.ycx.web.model.Player; @Configuration public class TestConfig { @Bean @Profile("prod") public Player player() { return new Player(); } @Bean @Profile("dev") public Painter painter() { return new Painter(); } }
2、激活profile
Spring根据两个独立的属性来确定激活那个profile。spring.profiles.active和spring.profiles.default。spring.profiles.active比spring.profiles.default优先级高,当设置了spring.profiles.active属性值那么就用它的值来确定激活那个profile;只有当spring.profiles.active没有设置的时候才会去查找spring.profiles.default属性值并用它的值来确定激活那个profile。当两个值都没设置时就没有激活的profile。
spring.profiles.active和spring.profiles.default可以为复数,多个值用逗号隔开。例如:spring.profiles.active=dev,play
有多种方式来设置这两个值:
1.作为DispatcherServlet的初始化参数
2.作为Web应用的上下文参数
3.作为JNDI条目
4.作为环境变量
5.作为JVM的系统属性
6.在集成测试环境上使用@ActiveProfiles注解
对于web应用个人喜欢在DispatcherServlet初始化参数和应用上下文参数指定spring.profiles.default值,通过环境变量指定spring.profiles.active值,这样就能在需要改变的地方轻松改变
<context-param> <param-name>spring.profiles.default</param-name> <param-value>prod</param-value> </context-param> <servlet> <servlet-name>ds</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- 也可通过classpath指定路径 --> <param-value>\WEB-INF\ds-servlet.xml</param-value> </init-param> <init-param> <param-name>spring.profiles.default</param-name> <param-value>prod</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
默认激活prod,通过下面的方式激活dev。
选用环境变量
windows系统
注意:我的机器重启系统之后才生效了
spring.profiles.active=dev
ubuntu系统
当前用户
$ vim ~/.bashrc 追加 export spring.profiles.active=dev $ source ~/.bashrc
所有用户
# vim /etc/profile 追加 export spring.profiles.active=dev # source /etc/profile
标注@Profile("dev")的Bean就会创建
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端