spring注解之@profile
spring中@profile与maven中的profile很相似,通过配置来改变参数。
例如在开发环境与生产环境使用不同的参数,可以配置两套配置文件,通过@profile来激活需要的环境,但维护两套配置文件不如maven中维护一套配置文件,在pom中通过profile来修改配置文件的参数来的实惠。
也有例外,比如我在开发中调用商城接口经常不能返回我需要的数据,每次都需要mock数据,所以我写了一个mock参数的借口调用类,在开发环境中就使用这个类,测试环境与生产环境则使用正常的借口调用类,这样就不用每次开发的时候去手动改一些代码。
注:@profile在3.2以后的版本支持方法级别和类级别,3.1版本只支持类级别。
言归正传,说下@profile使用方法。
一、注解配置
- /** 配置生产环境调用类 **/
- @service("productRpc")
- @profile("prop")
- public class ProductRpcImpl implements ProductRpc
- public String productBaseInfo(Long sku){
- return productResource.queryBaseInfo(Long sku);
- }
- }
- /** 配置生产环境调用类 **/
- @service("productRpc")
- @profile("dev")
- public class MockProductRpcImpl implements ProductRpc
- public String productBaseInfo(Long sku){
- return “iphone7”;
- }
- }
- /** 调用类 **/
- public class Demo(){
- @Resource(name="productRpc")
- private ProductRpc productRpc;
- public void demo(){
- String skuInfo = productRpc.productBaseInfo(123123L);
- logger.info(skuInfo);
- }
- }
这样就完成了基于注解的profile配置。当配置为生产环境的时候会正常调用接口,当为开发环境的时候回调用mock接口。
二、XML配置
- <!-- 开发环境 -->
- <beans profile="dev">
- <bean id="beanname" class="com.pz.demo.ProductRPC"/>
- </beans>
- <!-- 生产环境 -->
- <beans profile="dev">
- <bean id="beanname" class="com.pz.demo.MockProductRPC"/>
- </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)
- <context-param>
- <param-name>spring.profiles.default</param-name>
- <param-value>dev</param-value>
- </context-param>
2.作为DispatcherServlet的初始化参数
- ervlet>
- <servlet-name>springMVC</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:/spring-servlet.xml</param-value>
- </init-param>
- <init-param>
- <param-name>spring.profiles.default</param-name>
- <param-value>dev</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
3.spring-junit使用@ActiveProfiles进行激活
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "/spring-config.xml")
- @ActiveProfiles("dev")
- public class MainTest {
- ...
- }
4.作为JNDI条目
5.作为环境变量
6.作为JVM的系统属性
spring注解之@profile的更多相关文章
- Spring 3.1新特性之一:spring注解之@profile
前言 由于在项目中使用Maven打包部署的时候,经常由于配置参数过多(比如Nginx服务器的信息.ZooKeeper的信息.数据库连接.Redis服务器地址等),导致实际现网的配置参数与测试服务器参数 ...
- 【Spring Cloud】Spring Cloud之自定义@SpringCloudProfile注解实现@Profile注解的功能
一.为什么会想到定义@SpringCloudProfile这样的注解 首页提一下@Profile注解:它主要用与Spring Boot多环境配置中,指定某个类只在指定环境中生效,比如swagger的配 ...
- 【Spring】使用@Profile注解实现开发、测试和生产环境的配置和切换,看完这篇我彻底会了!!
写在前面 在实际的企业开发环境中,往往都会将环境分为:开发环境.测试环境和生产环境,而每个环境基本上都是互相隔离的,也就是说,开发环境.测试环境和生产环境是互不相通的.在以前的开发过程中,如果开发人员 ...
- 转-Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable
转-http://snowolf.iteye.com/blog/1628861/ Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariab ...
- Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable (转)
最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...
- Spring 快速开始 Profile 和 Bean
和maven profile类似,Spring bean definition profile 有两个组件:声明和激活. [栗子:开发测试环境使用HyperSQL 生产环境使用JNDI上下文根据配置查 ...
- Spring 环境与profile(一)——超简用例
什么是profile,为什么需要profile? 在开发时,不同环境(开发.联调.预发.正式等)所需的配置不同导致,如果每改变一个环境就更改配置不但麻烦(修改代码.重新构建)而且容易出错.Spring ...
- Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable(转)
最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札(一 ...
- 详解Spring中的Profile
前言 由于在项目中使用Maven打包部署的时候,经常由于配置参数过多(比如Nginx服务器的信息.ZooKeeper的信息.数据库连接.Redis服务器地址等),导致实际现网的配置参数与测试服务器参数 ...
随机推荐
- BZOJ2141 排队 树状数组 分块
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2141.html 题目传送门 - BZOJ2141 题意 给定一个序列 $a$ ,先输出原先的逆序对数. ...
- BZOJ2287 【POJ Challenge】消失之物 动态规划 分治
原文链接http://www.cnblogs.com/zhouzhendong/p/8684027.html 题目传送门 - BZOJ2287 题意 有$n$个物品,第$i$个物品的体积为$w_i$. ...
- Centos7使用yum命令安装Mysql5.6.X
首先:具体的安装步骤在mysql官方文档上都有详细的描述. 文档虽然是英文,不过很容易理解,我就不一一翻译了. 官方文档地址:https://dev.mysql.com/doc/refman/5.6/ ...
- Linux下C语言进程通讯编程
代码: #include <stdio.h> #include <stdlib.h> #include <sys/shm.h> /*************基本的函 ...
- Number String(DP)
题目: 题意: 给你一个字符串s,s[i] = 'D'表示排列中a[i] > a[i+1],s[i] = 'I'表示排列中a[i] < a[i+1]. 比如排列 {3, 1, 2, 7, ...
- C语言实现链栈
我自己写的代码部分: #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct ...
- vue的中vuex为何需要mutation更新状态,vue-router的路由的理解
); ); ); history.back(); history.forward(); // 修改历史,包括二个方法pushState.replaceState二个方法(objState,title, ...
- Sunscreen POJ - 3614(贪心)
To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with s ...
- poj 3067 Japan 【树状数组】
<题目链接> 题目大意: 有两个点集,这两个点集从上至下分别从1~n,1~m编号,现在给出n组数据,(x,y),表示左边点集编号为x的点与右边点集编号为y的点相连,现在要求计算这些线段的交 ...
- vue笔记-模板,计算属性,class与style,data属性
数据和方法 1:只有当实例被创建时 data 中存在的属性才是响应式的,也可以预定义一些空的属性,唯一的意外就是Object.freeze(obj),这会阻止修改现有的属性;也就是说一个数据在放到根实 ...