spring中@profile与maven中的profile很相似,通过配置来改变参数。

例如在开发环境与生产环境使用不同的参数,可以配置两套配置文件,通过@profile来激活需要的环境,但维护两套配置文件不如maven中维护一套配置文件,在pom中通过profile来修改配置文件的参数来的实惠。

也有例外,比如我在开发中调用商城接口经常不能返回我需要的数据,每次都需要mock数据,所以我写了一个mock参数的借口调用类,在开发环境中就使用这个类,测试环境与生产环境则使用正常的借口调用类,这样就不用每次开发的时候去手动改一些代码。

注:@profile在3.2以后的版本支持方法级别和类级别,3.1版本只支持类级别。

言归正传,说下@profile使用方法。

一、注解配置

  1. /** 配置生产环境调用类  **/
  2. @service("productRpc")
  3. @profile("prop")
  4. public class ProductRpcImpl implements ProductRpc
  5. public String productBaseInfo(Long sku){
  6. return productResource.queryBaseInfo(Long sku);
  7. }
  8. }
  9. /** 配置生产环境调用类  **/
  10. @service("productRpc")
  11. @profile("dev")
  12. public class MockProductRpcImpl implements ProductRpc
  13. public String productBaseInfo(Long sku){
  14. return “iphone7”;
  15. }
  16. }
  17. /** 调用类  **/
  18. public class Demo(){
  19. @Resource(name="productRpc")
  20. private ProductRpc productRpc;
  21. public void demo(){
  22. String skuInfo = productRpc.productBaseInfo(123123L);
  23. logger.info(skuInfo);
  24. }
  25. }

这样就完成了基于注解的profile配置。当配置为生产环境的时候会正常调用接口,当为开发环境的时候回调用mock接口。

二、XML配置

  1. <!-- 开发环境 -->
  2. <beans profile="dev">
  3. <bean id="beanname" class="com.pz.demo.ProductRPC"/>
  4. </beans>
  5. <!-- 生产环境 -->
  6. <beans profile="dev">
  7. <bean id="beanname" class="com.pz.demo.MockProductRPC"/>
  8. </beans>

三、激活profile

注:spring在确定那个profile处于激活状态的时,需要依赖两个独立的属性:spring.profiles.active和spring.profile.default。如果设置了spring.profiles.actives属性,那么它的值就会用来确定那个profile是激活的。如果没有设置spring.profiles.active属性的话,那spring将会查找spring.profiles.default的值。如果spring.profiles.active和spring.profiles.default均没有设置。(红色部分未在项目中验证成功,待测试)

1.在servlet上下文中进行配置(web.xml)

  1. <context-param>
  2. <param-name>spring.profiles.default</param-name>
  3. <param-value>dev</param-value>
  4. </context-param>

2.作为DispatcherServlet的初始化参数

  1. ervlet>
  2. <servlet-name>springMVC</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <init-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:/spring-servlet.xml</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>spring.profiles.default</param-name>
  10. <param-value>dev</param-value>
  11. </init-param>
  12. <load-on-startup>1</load-on-startup>
  13. </servlet>

3.spring-junit使用@ActiveProfiles进行激活

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = "/spring-config.xml")
  3. @ActiveProfiles("dev")
  4. public class MainTest {
  5. ...
  6. }

4.作为JNDI条目

5.作为环境变量

6.作为JVM的系统属性

spring注解之@profile的更多相关文章

  1. Spring 3.1新特性之一:spring注解之@profile

    前言 由于在项目中使用Maven打包部署的时候,经常由于配置参数过多(比如Nginx服务器的信息.ZooKeeper的信息.数据库连接.Redis服务器地址等),导致实际现网的配置参数与测试服务器参数 ...

  2. 【Spring Cloud】Spring Cloud之自定义@SpringCloudProfile注解实现@Profile注解的功能

    一.为什么会想到定义@SpringCloudProfile这样的注解 首页提一下@Profile注解:它主要用与Spring Boot多环境配置中,指定某个类只在指定环境中生效,比如swagger的配 ...

  3. 【Spring】使用@Profile注解实现开发、测试和生产环境的配置和切换,看完这篇我彻底会了!!

    写在前面 在实际的企业开发环境中,往往都会将环境分为:开发环境.测试环境和生产环境,而每个环境基本上都是互相隔离的,也就是说,开发环境.测试环境和生产环境是互不相通的.在以前的开发过程中,如果开发人员 ...

  4. 转-Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable

    转-http://snowolf.iteye.com/blog/1628861/ Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariab ...

  5. Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable (转)

    最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...

  6. Spring 快速开始 Profile 和 Bean

    和maven profile类似,Spring bean definition profile 有两个组件:声明和激活. [栗子:开发测试环境使用HyperSQL 生产环境使用JNDI上下文根据配置查 ...

  7. Spring 环境与profile(一)——超简用例

    什么是profile,为什么需要profile? 在开发时,不同环境(开发.联调.预发.正式等)所需的配置不同导致,如果每改变一个环境就更改配置不但麻烦(修改代码.重新构建)而且容易出错.Spring ...

  8. Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable(转)

    最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...

  9. 详解Spring中的Profile

    前言 由于在项目中使用Maven打包部署的时候,经常由于配置参数过多(比如Nginx服务器的信息.ZooKeeper的信息.数据库连接.Redis服务器地址等),导致实际现网的配置参数与测试服务器参数 ...

随机推荐

  1. appium---第四个脚本,进入app,有权限弹窗的方法

    1.以淘宝为例:进入首页,会弹出好几个权限弹窗 无法使用id定位 用xpath定位

  2. BZOJ2141 排队 树状数组 分块

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2141.html 题目传送门 - BZOJ2141 题意 给定一个序列 $a$ ,先输出原先的逆序对数. ...

  3. Python中GIL

    GIL(global interpreter lock)全局解释器锁 python中GIL使得同一个时刻只有一个线程在一个cpu上执行,无法将多个线程映射到多个cpu上执行,但GIL并不会一直占有,它 ...

  4. spring mvc读取properties资源文件夹中文乱码问题

    通过在applicationContext.xml和springmvc.xml中配置 <bean        class="org.springframework.beans.fac ...

  5. html-模仿小米首页定位案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. datetime.timedelta类

    datetime.timedelta对象代表两个时间之间的时间差,两个date或datetime对象相减就可以返回一个timedelta对象. Python中datetime模块中的timedelta ...

  7. hdu 3499 flight 【分层图】+【Dijkstra】

    <题目链接> 题目大意: 现在给你一些点,这些点之间存在一些有向边,每条边都有对应的边权,有一次机会能够使某条边的边权变为原来的1/2,求从起点到终点的最短距离. 解题分析: 分层图最短路 ...

  8. hdu1429 胜利大逃亡(续) 【BFS】+【状态压缩】

    题目链接:https://vjudge.net/contest/84620#problem/K 题目大意:一个人从起点走到终点,问他是否能够在规定的时间走到,在走向终点的路线上,可能会有一些障碍门,他 ...

  9. C# 的Chart

    Axis Label 横纵坐标的文字 (比如 0 20 40 ....) Axis Title 横纵坐标的代表什么(比如 Y Axis Title) Chart Area 图标所在位置 Chart P ...

  10. 字节跳动冬令营网络赛 D.The Easiest One(贪心 数位DP)

    题目链接 \(x:\ 11010011\) \(y:\ 10011110\) (下标是从高位往低位,依次是\(1,2,...,n\)) 比如对于这两个数,先找到最高的满足\(x\)是\(0\),\(y ...