Spring注解@profile的使用
spring中@profile与maven中的profile很相似,通过配置来改变参数。
例如在开发环境与生产环境使用不同的参数,可以配置两套配置文件,通过@profile来激活需要的环境,但维护两套配置文件不如maven中维护一套配置文件,在pom中通过profile来修改配置文件的参数来的实惠。
也有例外,比如我在开发中调用商城接口经常不能返回我需要的数据,每次都需要mock数据,所以我写了一个mock参数的借口调用类,在开发环境中就使用这个类,测试环境与生产环境则使用正常的借口调用类,这样就不用每次开发的时候去手动改一些代码。
注:@profile在3.2以后的版本支持方法级别和类级别,3.1版本只支持类级别。
言归正传,说下@profile在spring mvc和spring boot的用法。
Spring mvc:
一、注解配置
/** 配置开发环境调用类 **/ @service("product") @profile("dev") public class ProductRpcImpl implements ProductRpc public String productBaseInfo(Long sku){ return productResource.queryBaseInfo(Long sku); } } /** 配置生产环境调用类 **/ @service("product")
@profile("prop")
public class MockProductRpcImpl implements ProductRpc public String productBaseInfo(Long sku){ return “iphone7”; } }
二、xml配置
<!-- 开发环境 --> <beans profile="dev"> <bean id="beanname" class="com.demo.Product"/> </beans> <!-- 生产环境 --> <beans profile="prop"> <bean id="beanname" class="com.demo.MockProduct"/> </beans>
三、激活配置
1.在servlet上下文中进行配置(web.xml)
<context-param> <param-name>spring.profiles.default</param-name> <param-value>dev</param-value> </context-param>
2.作为DispatcherServlet的初始化参数
<servlet> <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>
Spring boot:
1、注解配置(类似mvc)
//在这个例子中,我们就能为 SecurityConfig 加上 @Profile 注解:
@Profile("production")
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
}
2、配置和激活(在application.properities/application.yml中)
运行时设置 spring.profiles.active 属性就能激活Profile,任意设置配置属性的方式都能用于设置这个值。例如,在命令行里运行应用程序时,可以这样激活 production Profile:
$ java -jar readinglist-0.0.1-SNAPSHOT.jar --spring.profiles.active=production
也可以向application.properities/application.yml里添加 spring.profiles.active 属性:
spring:profiles:active: production
yml的配置切换:
logging:level:root: INFO
---
spring:profiles: developmentlogging:level:root: DEBUG
---
spring:profiles: productionlogging:path: /tmp/file: BookWorm.loglevel:root: WARN
Spring注解@profile的使用相关推荐
- Spring注解——@Profile详解
目录 一.Spring中的Profile 二.为什么要使用Profile 三.配置Profile示例 四.激活Profile的方式 一.Spring中的Profile Spring中的Profile功 ...
- 0、Spring 注解驱动开发
0.Spring注解驱动开发 0.1 简介 <Spring注解驱动开发>是一套帮助我们深入了解Spring原理机制的教程: 现今SpringBoot.SpringCloud技术非常火热,作 ...
- Spring 注解 @bean 和 @component 的区别, 你知道吗?
本文打算介绍几个不太容易说出其区别,或者用途的 Spring 注解,比如 @Component 与 @Bean 的比较,@ControllerAdvice 是如何处理自定义异常的等等. Spring ...
- Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable(转)
最近需要做些接口服务,服务协议定为JSON,为了整合在Spring中,一开始确实费了很大的劲,经朋友提醒才发现,SpringMVC已经强悍到如此地步,佩服! 相关参考: Spring 注解学习手札( ...
- 浅谈Spring注解
Spring目前的趋势是使用注解结合Java代码而不是配置来定义行为.属性.功能.规则和扩展点,因此梳理注解也是梳理Spring功能点的很好的方式,全面的梳理可以补足我们知识点的漏洞. 查找所有注解 ...
- Spring实战——Profile
看到Profile这个关键字,或许你从来没有正眼瞧过他,又或者脑海中有些模糊的印象,比如除了这里Springmvc中的Profile,maven中也有Profile的标签. 从字面意思来看,Profi ...
- 设计模式——Spring注解编程模型
文章目录 1. 引言 2. Spring注解编程模型 2.1 元注解(Meta-Annotations) 2.2 Spring模式注解(Stereotype Annotations) 2.3 Spri ...
- Spring注解开发学习笔记
1 IOC 1.1 工厂模式 使用工厂中方法代替new形式创建对象的一种设计模式 1.2 Inversion of Control控制翻转 一种思想,用于消减代码间的耦合. 实现思想:利用工厂设计模式 ...
- spring注解及扩展
1,spring配置注解 spring建议通过注解配置,替代原xml配置方式. 使用配置类替代xml配置的优势大体: 1,xml配置维护容易出错而且不易检查,java配置类基于java语法检查,对于j ...
最新文章
- 【Java】Lucene检索引擎详解
- 如何调用webservice接口
- Linkis计算中间件部署过程记录
- java ee ide 假死_Eclipse编辑jsp、js文件时卡死现象的解决办法汇总
- 三种查看SqlServer中数据物理pge页的方法
- linux创建特殊文件rules,RHEL5 Oracle Linux 5上生成正确的udev rule 规则文件
- 12.2日,第二次团队冲刺开始
- git与github学习笔记
- 电商如何“链”动新春,看聚划算助力“原年人”的好日子
- mysql 完整性概念_mysql基础知识
- 计算机科学与技术的应用图,安徽农业大学计算机科学与技术视图及其应用.ppt...
- pdf转swf linux,Linux环境安装配置Swftools,pdf转swf工具
- 用大数据挑选出国外最值得看的前50条swift教程(v.2019)
- Caused by: java.lang.SecurityException: com.example.geofences from uid 10049 not allowed to perform
- .har 文件解析工具
- win的名词_英语语法系列:名词性从句
- cd40系列芯片_CD40,CD45系列芯片功能大全
- Cygwin系列(一):Cygwin是什么
- numeric_limits 解析
- 2021中国IC封装基板市场现状及未来发展趋势