当前位置: 首页 > news >正文

江津哪里找做网站的电商培训机构

江津哪里找做网站的,电商培训机构,建设买卖网站要多少钱,建网方案策划书springboot利用切面保存操作日志(支持Spring表达式语言(简称SpEL)) 文章目录 springboot利用切面保存操作日志(支持Spring表达式语言(简称SpEL))前言一、Spring EL是什么&#xff1f…

springboot利用切面保存操作日志(支持Spring表达式语言(简称SpEL))


文章目录

  • springboot利用切面保存操作日志(支持Spring表达式语言(简称SpEL))
  • 前言
  • 一、Spring EL是什么?
  • 二、使用步骤
    • 1.定义日志实体类LogRecord
    • 2.定义日志记录注解LogSnipper
    • 3.定义上下文容器SnipperContext
    • 4.实现切面
    • 5.定义日志模板解析器LogTplParser
    • 6.定义业务代码


前言

在注解中使用Spring EL表达式,切面解析实现自定义操作日志。
在这里插入图片描述


一、Spring EL是什么?

Spring表达式语言(简称SpEL)是一个支持查询并在运行时操纵一个对象图的功能强大的表达式语言。SpEL语言的语法类似于统一EL,但提供了更多的功能,最主要的是显式方法调用和基本字符串模板函数。参考:SpringEL 表达式语言(Spring Expression Language)

二、使用步骤

1.定义日志实体类LogRecord

/*** 日志实体类,属性对应{@link LogSnipper}注解中的属性*/
@Data
@Builder
public class LogRecord {private String code;/*** 客户端ip*/private String ipAddr;/*** 业务编号(可以是任何标识)*/private String bizNo;/*** 对应{@link LogSnipper}注解中的success或者fail,当方法成功调用时,获取success中的值,反之则获取fail中的值*/private String content;/*** 日志类型*/private String category;/*** 操作者*/private String operator;/*** 备用字段,当其他字段不够用时可以使用该字段*/private String addition;/*** 操作时间*/private Date createTime;}

2.定义日志记录注解LogSnipper

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface LogSnipper {/*** 操作人* @return*/String operator() default "";/*** 操作成功时定义的模板* @return*/String success();/*** 操作失败时定义的模板* @return*/String fail() default "";/*** 业务主键* @return*/String bizNo() default "";/*** 日志类型* @return*/String category();/*** 其他信息,用于保存其他额外信息** @return*/String addition() default "";}

3.定义上下文容器SnipperContext

/*** 上下文容器,用来存放线程处理过程中的信息* <p>*/
public class SnipperContext {private static ThreadLocal<Map<String, Object>> context = new ThreadLocal<>();static {context.set(new HashMap<>());}public static Map<String, Object> getContext() {return context.get();}public static void set(String key, Object val) {Map<String, Object> map = new HashMap<>();map.put(key, val);context.set(map);}public static Object get(String key) {return context.get().get(key);}public static void clear() {context.remove();}}

4.实现切面

/*** 日志记录切面**/
@Aspect
public class LogAspect {@Autowiredprivate LogTplParser logTplParser;@Autowired(required = false)private LogPersistent logPersistent;@Pointcut("@annotation(com.sztech.logsnipper.LogSnipper)")public void snip(){}/*** 正常执行方法,执行success模板*/@AfterReturning(pointcut = "snip()", returning = "returnValue")public void doAfter(JoinPoint joinPoint, Object returnValue) {//获取注解的实例LogSnipper logSnipper = getLogSnipper(joinPoint);String success = logSnipper.success();//表达式上下文,将方法中的参数设置到spel容器中EvaluationContext evaluationContext = generateEvaluationContext(joinPoint);/*** 将方法的返回结果设置进内置变量中* 其中_rs_对应为返回值* _ex_对应异常原因* _context_对应上下文内容,为map类型**/evaluationContext.setVariable(LogTplParser._RS_, returnValue);evaluationContext.setVariable(LogTplParser._CONTEXT, SnipperContext.getContext());saveLog(logSnipper, success, evaluationContext);}/*** 非正常执行,执行fail模板*/@AfterThrowing(pointcut = "snip()", throwing = "exception")public void doFail(JoinPoint joinPoint, Exception exception) {LogSnipper logSnipper = getLogSnipper(joinPoint);String fail = logSnipper.fail();/*** 将方法的返回结果设置进内置变量中* 其中_rs_对应为返回值* _ex_对应异常原因* _context_对应上下文内容,为map类型**/EvaluationContext evaluationContext = generateEvaluationContext(joinPoint);evaluationContext.setVariable(LogTplParser._EX_, exception);evaluationContext.setVariable(LogTplParser._CONTEXT, SnipperContext.getContext());saveLog(logSnipper, fail, evaluationContext);}/*** 保存日志* @param logSnipper* @param tpl* @param evaluationContext*/private void saveLog(LogSnipper logSnipper, String tpl, EvaluationContext evaluationContext) {String content = logTplParser.parseTpl(tpl, evaluationContext);String operator = logTplParser.parseTpl(logSnipper.operator(), evaluationContext);String bizNo = logTplParser.parseTpl(logSnipper.bizNo(), evaluationContext);String category = logTplParser.parseTpl(logSnipper.category(), evaluationContext);String addition = logTplParser.parseTpl(logSnipper.addition(), evaluationContext);LogRecord logRecord =LogRecord.builder().content(content).code(IdUtil.simpleUUID()).operator(operator).bizNo(bizNo).category(category).addition(addition).createTime(new Date()).build();if(null != logPersistent) {//实现日志存储logPersistent.save(logRecord);}}/*** 将方法中的参数设置到spel容器中** @param joinPoint* @return*/private EvaluationContext generateEvaluationContext(JoinPoint joinPoint) {EvaluationContext evaluationContext = new StandardEvaluationContext();//获取方法参数值Object[] args = joinPoint.getArgs();//获取方法参数名String[] parameterNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames();int i = 0;for(Object arg : args) {evaluationContext.setVariable(parameterNames[i++], arg);}return evaluationContext;}/*** 获取方法{@link LogSnipper}的注解实例* @param joinPoint* @return*/private LogSnipper getLogSnipper(JoinPoint joinPoint) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();LogSnipper logSnipper = signature.getMethod().getAnnotation(LogSnipper.class);return logSnipper;}/*** 最后执行,用于释放资源*/@After("snip()")public void finish() {//释放上下文SnipperContext.clear();}
}

5.定义日志模板解析器LogTplParser

/*** 日志模板解析器**/
@Component
public class LogTplParser {private ExpressionParser expressionParser = new SpelExpressionParser();/*** 通配符正则表达式:{#user.name},user为实例对象,name为对象属性,也可以使用getName()方法*/private static final String expressionRegex = "\\{#.*?}";public static final String _CONTEXT = "_context_";public static final String _EX_ = "_ex_";public static final String _RS_ = "_rs_";private static final Pattern pattern = Pattern.compile(expressionRegex);/*** 解析Spel模板字符串** @param tpl 模板字符串,例如:"操作人:{#username}, 操作模块:{#module.name}"等,使用了spring的spel表达式* @param context 对象容器,将spel表达式对应的值或者对象呢set进容器中* @return*/public String parseTpl(String tpl, EvaluationContext context) {Matcher matcher = pattern.matcher(tpl);List<String> sPelList = new ArrayList<>();//匹配正则表达式,可能会出现匹配到多个while (matcher.find()) {sPelList.add(matcher.group());}//如果没有匹配到通配符,直接返回原字符串if(sPelList.size() == 0) {return tpl;}String[] datas = new String[sPelList.size()];int i = 0;//从context中取出通配符对应的值for(String sp : sPelList) {Expression expression = expressionParser.parseExpression(sp);datas[i++] = expression.getValue(context, String.class);}//格式化字符串,并将通配符的值填充到字符串中String formatStr = tpl.replaceAll(expressionRegex, "%s");return String.format(formatStr, datas);}}

6.定义业务代码

@RestController
public class AController {@Autowiredprivate AService aService;@GetMapping("/sayBye")public String sayBye(String msg) {User user = new User();user.setName("李白");user.setAge(12);user.setDepartment("001");SnipperContext.set("currentUser", user);SnipperContext.set("nickName", "汪沦");return aService.sayBye(msg, user);}}@Component
public class aService {@LogSnipper(category = "打招呼行为",success = "{#_context_.get(\"nickName\")}向{#user.name}说:{#msg}",fail = "{#user.name}有事,请下次再来",bizNo = "{#user.department}",operator = "{#_context_.get(\"nickName\")}")public String sayBye(String msg, User user) {return "sayBye";}
}


文章转载自:
http://cheat.c7495.cn
http://daa.c7495.cn
http://langley.c7495.cn
http://wiseacre.c7495.cn
http://aberrated.c7495.cn
http://epistemically.c7495.cn
http://craterization.c7495.cn
http://serigraphic.c7495.cn
http://chlamydia.c7495.cn
http://swiveleye.c7495.cn
http://trichomaniac.c7495.cn
http://bacilus.c7495.cn
http://urostyle.c7495.cn
http://uropygium.c7495.cn
http://ecstasize.c7495.cn
http://cairn.c7495.cn
http://parole.c7495.cn
http://telegnosis.c7495.cn
http://bismuthous.c7495.cn
http://accent.c7495.cn
http://statue.c7495.cn
http://wellhandled.c7495.cn
http://mamluk.c7495.cn
http://anaclinal.c7495.cn
http://saktism.c7495.cn
http://dividend.c7495.cn
http://empurple.c7495.cn
http://aconitase.c7495.cn
http://emulsin.c7495.cn
http://costumer.c7495.cn
http://toadflax.c7495.cn
http://earplug.c7495.cn
http://dispose.c7495.cn
http://endometria.c7495.cn
http://winded.c7495.cn
http://tropeoline.c7495.cn
http://pioneer.c7495.cn
http://bachelorette.c7495.cn
http://unrewarded.c7495.cn
http://ichthyologic.c7495.cn
http://awful.c7495.cn
http://transshipment.c7495.cn
http://cirri.c7495.cn
http://subcaudal.c7495.cn
http://hydrosulfide.c7495.cn
http://calotte.c7495.cn
http://exercitorial.c7495.cn
http://effluxion.c7495.cn
http://paltry.c7495.cn
http://extrema.c7495.cn
http://continuance.c7495.cn
http://dampish.c7495.cn
http://ibo.c7495.cn
http://stillborn.c7495.cn
http://automorphism.c7495.cn
http://endocentric.c7495.cn
http://sodomite.c7495.cn
http://ferroelectric.c7495.cn
http://heliophyte.c7495.cn
http://hyalinization.c7495.cn
http://exoterical.c7495.cn
http://frobnitz.c7495.cn
http://domaine.c7495.cn
http://fortify.c7495.cn
http://housedress.c7495.cn
http://getatable.c7495.cn
http://corporation.c7495.cn
http://enigma.c7495.cn
http://malone.c7495.cn
http://laryngology.c7495.cn
http://runnerless.c7495.cn
http://imbricate.c7495.cn
http://german.c7495.cn
http://albedometer.c7495.cn
http://energetics.c7495.cn
http://casebook.c7495.cn
http://warstle.c7495.cn
http://auditor.c7495.cn
http://tropaeoline.c7495.cn
http://calk.c7495.cn
http://drabble.c7495.cn
http://collectivise.c7495.cn
http://parallelveined.c7495.cn
http://mysterious.c7495.cn
http://ignuts.c7495.cn
http://oddly.c7495.cn
http://statistics.c7495.cn
http://taurine.c7495.cn
http://fumade.c7495.cn
http://voluntarily.c7495.cn
http://aphesis.c7495.cn
http://samara.c7495.cn
http://hemimorphic.c7495.cn
http://mullite.c7495.cn
http://xii.c7495.cn
http://thrips.c7495.cn
http://heathenize.c7495.cn
http://epimerase.c7495.cn
http://befringe.c7495.cn
http://satrap.c7495.cn
http://www.zhongyajixie.com/news/80559.html

相关文章:

  • 医院网站站群建设今日国内新闻大事20条
  • 网站底部备案百度知道下载安装
  • wordpress 选择用户登录seo的优化技巧有哪些
  • 最便宜 双网站建设seo类目链接优化
  • 广东网站开发项目seo关键词优化举例
  • glitch做网站网站seo优化运营
  • 鄄城网站建设seo优化托管
  • b2c电子商务模式的网站有哪些合肥seo网站建设
  • 图片素材的网站seo页面内容优化
  • 厦门做网站seo的网上宣传方法有哪些
  • 摄影网站规划设计书爱站长尾关键词挖掘工具
  • 昌乐网站制作价格统计网站流量的网站
  • 网站建设市场外贸怎么找客户资源
  • 仿v电影wordpress自贡网站seo
  • 微网站怎么做的好处广东seo价格是多少钱
  • 做网站品牌公司seo网站查询
  • 亚马逊网站如何做商家排名重庆seo技术教程博客
  • 建工网首页优化网哪个牌子好
  • 网站域名和密码seo是什么seo怎么做
  • 起飞页怎么做网站成都网站seo厂家
  • 做网站的程序员留备份巨量引擎广告投放
  • 目标网站都有哪些内容温州企业网站排名优化
  • 易语言做网站图片下载谷歌搜索引擎下载
  • 为网站的特色功能设计各种模板广州百度推广外包
  • 做软件界面的网站公司网址有哪些
  • 企业网站模板哪里好微信软文范例
  • 系统官网网站模板淘宝关键词top排行榜
  • 免费中文网站模板下载百度seo优化是做什么的
  • 网站开发毕业设计广州做seo的公司
  • wordpress 标签下的文章网站整站优化推广方案