spring注解之@profile
来源:互联网 发布:江苏商贸职业学院网络 编辑:程序博客网 时间:2024/05/21 18:43
spring中@profile与maven中的profile很相似,通过配置来改变参数。
例如在开发环境与生产环境使用不同的参数,可以配置两套配置文件,通过@profile来激活需要的环境,但维护两套配置文件不如maven中维护一套配置文件,在pom中通过profile来修改配置文件的参数来的实惠。
言归正传,说下@profile使用方法。
二、XML配置
三、激活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)
5.作为环境变量
6.作为JVM的系统属性
例如在开发环境与生产环境使用不同的参数,可以配置两套配置文件,通过@profile来激活需要的环境,但维护两套配置文件不如maven中维护一套配置文件,在pom中通过profile来修改配置文件的参数来的实惠。
也有例外,比如我在开发中调用商城接口经常不能返回我需要的数据,每次都需要mock数据,所以我写了一个mock参数的借口调用类,在开发环境中就使用这个类,测试环境与生产环境则使用正常的借口调用类,这样就不用每次开发的时候去手动改一些代码。
言归正传,说下@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的系统属性
0 1
- spring注解之@profile
- spring @profile注解的使用
- Spring注解@Profile与@Conditional
- spring @profile注解的使用
- 关于Spring的@Profile注解的使用记录
- 注解之Spring
- Spring之注解
- spring之注解
- Spring之注解
- Spring 注解 之 @Transactional
- Spring注解之事务管理
- Spring注解之:@RestController
- Spring注解之:@SpringBootApplication
- Spring MVC之 注解
- spring注解之@SessionAttributes
- spring框架之注解
- spring之注解开发
- spring之注解
- java 判断是否中文字符
- 公式编辑器如何调整矩阵分隔线的粗线细
- java写代码使注意什么
- 浅析:如何选择关键词
- Sqlmap的使用(1)
- spring注解之@profile
- 序列化和反序列化
- Vim 实现复制多行
- webservice概述
- ArcGIS服务器的feature图层限制
- JAVA:内层方法抛出异常 外层方法捕捉并处理异常
- 文章收集贴
- 生成全排列的c++函数
- 单文档中其它类反调视图类中函数的方法