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

山西住房和建设厅网站国内免费建网站

山西住房和建设厅网站,国内免费建网站,微网站介绍,最新国际新闻事件一、前言 在上篇文章中,我们使用了AOP思想实现日志记录的功能,代码中采用了指定连接点方式(Pointcut(“execution(* com.nowcoder.community.controller..(…))”)),指定后不需要在进行任何操作就可以记录日志了&…

一、前言

在上篇文章中,我们使用了AOP思想实现日志记录的功能,代码中采用了指定连接点方式(@Pointcut(“execution(* com.nowcoder.community.controller..(…))”)),指定后不需要在进行任何操作就可以记录日志了,但是如果我们对某些controller不想记录日志,就需要更改指定的切点,灵活性较差。因此采用注解+AOP方式,实现更灵活的日志记录功能。

二、注解实现代码

package com.nowcoder.community.annotation;import java.lang.annotation.*;/*** @author Janson* @Description AOP日志记录注解* @Date 2023/5/12* @Target LogAspect 注解的作用目标* @Retention 指定注解的保留时间*/@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogAspect {/*** title 自定义名称* description 自定义描述* @return*/String title() default "";String description() default "";
}

三、AOP日志记录类实现代码

定义切点(织入点)
execution(* com.nowcoder.community.controller..(…))
- 第一个 * 表示 支持任意类型返回值的方法
- com.nowcoder.community.controller 表示这个包下的类
- 第二个 * 表示 controller包下的任意类
- 第三个 * 表示 类中的任意方法
- (…) 表示方法可以拥有任意参数
可以根据自己的需求替换。

package com.nowcoder.community.aspect;import com.nowcoder.community.annotation.LogAspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;/*** @author Janson* @Description AOP日志记录,此方案不统一设置连接点 @Pointcut,而是采用注解方式,指定连接的方法* @Date 2023/5/12*/
@Aspect
@Component
public class LogAspectAnnotationTest {/*** 定义切点(织入点)*  execution(* com.nowcoder.community.controller.*.*(..))*      - 第一个 * 表示 支持任意类型返回值的方法*      - com.nowcoder.community.controller 表示这个包下的类*      - 第二个 * 表示 controller包下的任意类*      - 第三个 * 表示 类中的任意方法*      - (..) 表示方法可以拥有任意参数*   可以根据自己的需求替换。**/@Before(value = "@annotation(controllerLog)")public void before(JoinPoint joinPoint, LogAspect controllerLog){String className = joinPoint.getClass().getName();String methodName = joinPoint.getSignature().getName();ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if (attributes == null){return;}String ip = attributes.getRequest().getRemoteHost();String requestURI = attributes.getRequest().getRequestURI();String requestMethod = attributes.getRequest().getMethod();String title = controllerLog.title();String description = controllerLog.description();System.out.println("title is " + title + ",description is " + description);System.out.println("before excute ······");System.out.println(String.format("用户: [%s] 的请求路径为:[%s], 请求方式为: [%s],请求类名为: [%s], 请求方法名为: [%s]", ip,requestURI,requestMethod,className,methodName));}@After(value = "@annotation(controllerLog)")public void after(JoinPoint joinPoint, LogAspect controllerLog){String className = joinPoint.getClass().getName();String methodName = joinPoint.getSignature().getName();ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if (attributes == null){return;}String ip = attributes.getRequest().getRemoteHost();String requestURI = attributes.getRequest().getRequestURI();String requestMethod = attributes.getRequest().getMethod();String title = controllerLog.title();String description = controllerLog.description();System.out.println("title is " + title + ",description is " + description);System.out.println("after excute ······");System.out.println(String.format("用户: [%s] 的请求路径为:[%s], 请求方式为: [%s],请求类名为: [%s], 请求方法名为: [%s]", ip,requestURI,requestMethod,className,methodName));}@AfterReturning(value = "@annotation(controllerLog)")public void afterReturning(JoinPoint joinPoint, LogAspect controllerLog){String className = joinPoint.getClass().getName();String methodName = joinPoint.getSignature().getName();ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if (attributes == null){return;}String ip = attributes.getRequest().getRemoteHost();String requestURI = attributes.getRequest().getRequestURI();String requestMethod = attributes.getRequest().getMethod();String title = controllerLog.title();String description = controllerLog.description();System.out.println("title is " + title + ",description is " + description);System.out.println("afterReturning excute ······");System.out.println(String.format("用户: [%s] 的请求路径为:[%s], 请求方式为: [%s],请求类名为: [%s], 请求方法名为: [%s]", ip,requestURI,requestMethod,className,methodName));}@AfterThrowing(value = "@annotation(controllerLog)",throwing = "e")public void afterThrowing(JoinPoint joinPoint, LogAspect controllerLog,Exception e){String className = joinPoint.getClass().getName();String methodName = joinPoint.getSignature().getName();ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();if (attributes == null){return;}String ip = attributes.getRequest().getRemoteHost();String requestURI = attributes.getRequest().getRequestURI();String requestMethod = attributes.getRequest().getMethod();String title = controllerLog.title();String description = controllerLog.description();System.out.println("title is " + title + ",description is " + description);System.out.println("afterThrowing excute ······" + e.getMessage());System.out.println(String.format("用户: [%s] 的请求路径为:[%s], 请求方式为: [%s],请求类名为: [%s], 请求方法名为: [%s],请求失败原因: [%s]", ip,requestURI, requestMethod,className,methodName,e.getMessage()));}@Around(value = "@annotation(controllerLog)")public void around(ProceedingJoinPoint joinPoint, LogAspect controllerLog) throws Throwable {String className = joinPoint.getTarget().getClass().getName();String methodName = joinPoint.getSignature().getName();System.out.println("className is : " + className + ". methodName is : " + methodName);ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();if (attributes == null){return;}// 请求ipString ip = attributes.getRequest().getRemoteHost();// 请求路径String requestURI = attributes.getRequest().getRequestURI();// 请求方法String requestMethod = attributes.getRequest().getMethod();String title = controllerLog.title();String description = controllerLog.description();System.out.println(String.format("用户: [%s] 的请求路径为:[%s], 请求方式为: [%s],请求类名为: [%s], 请求方法名为: [%s]", ip,requestURI,requestMethod,className,methodName));Object obj = null;try {obj = joinPoint.proceed();} catch (Throwable e) {System.out.println("我捕获了异常");throw new RuntimeException("执行失败",e);}System.out.println(String.format("用户: [%s] 的请求路径为:[%s], 请求方式为: [%s],请求类名为: [%s], 请求方法名为: [%s]", ip,requestURI,requestMethod,className,methodName));System.out.println("around excute after ········");}
}
http://www.dt0577.cn/news/34729.html

相关文章:

  • asp.netc 动态网站开发网站制作多少钱一个
  • 济宁网站建设神华百度关键词查询排名
  • 凌云县城乡建设局网站手机卡顿优化软件
  • 网站建设相对应的税收分类是免费seo教程资源
  • 手机如何做车载mp3下载网站hyein seo是什么牌子
  • wordpress管理界面seo推广优化方案
  • h5都用什么网站seoul是啥意思
  • 金华网站建设外包手机维修培训班学校
  • 如何再国外网站做折扣目前推广平台都有哪些
  • 深圳 网站设计网盘资源共享网站
  • 无锡网站建设有限公司市场营销实际案例
  • 郑州网站建设汉狮老客外链
  • 十大门户网站有哪些广州网站建设推荐
  • 厦门网站建设哪家厦门建设银行seo排名工具提升流量
  • 建设书局 网站四川最好的网络优化公司
  • 有哪些网站可以免费做外销如何让百度收录自己信息
  • 东莞企业网站电话seo基本步骤
  • wordpress网站插件下载失败品牌传播方案
  • 软件公司网站系统集成建设短视频seo关键词
  • 自己做网站卖机器设备北京seo运营
  • 陕西网站建设策划内容关键词检测
  • 什么源码做有趣的网站搜索引擎优化简称
  • 做谷歌网站制作网站需要的技术与软件
  • vf建设银行网站新闻稿
  • thinkphp大型网站开发推销产品的万能句子
  • 如何做内网站的宣传栏精准网络营销推广
  • 住房和城乡建设部网站唐山手机网站制作
  • 东莞网站建设价格长沙网站优化排名推广
  • 网站开发子账号抖音怎么推广引流
  • 做网站go和pythongoogle官网进入