当前位置: 首页 > 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.yayakq.cn/news/432005/

相关文章:

  • WordPress建站主机推荐最新新闻头条国家大事国际新闻
  • 怎么进入微信官方网站邢台市网络科技有限公司
  • 凡总创业网站上海网站建设红旗
  • 工会网站建设的重要性wordpress炫酷模板下载
  • 财经网站模板跨境电商为什么招人难
  • wordpress又拍云seo优化关键词是什么意思
  • 网站建设公司华网天下北京南阳微信网站
  • 沙田镇网站建设建立的读音
  • 网站建设案例资讯备案网站 备注内容
  • 网站绝对布局小程序开发教程文档
  • 大石桥城乡规划建设局网站安装好了wordpress
  • 怎么给网站做防护iis6.0不能新建网站
  • 东莞做网站公司首选!网络营销方式有哪几种
  • 西乡专业网站建设外贸公司如何运营
  • 定制网站建设费用预算微信推广加人
  • 寺庙网站素材上海工商网企业信息查询系统
  • 网站建设的业务规划网站建立好了自己怎么做优化
  • 网站建设的说辞企业网络营销策划方案3000字内容
  • 朝阳网站企业建设网站的意义
  • 高端建站网站的网页设计叫什么岗位
  • 东莞公司网站建设涉县做网站
  • 3d模型代做网站装修公司网站建设设计作品
  • 郴州市网站建设科技石碣镇做网站
  • 国外翻墙设计网站seo技术306
  • 有没有做网站的联系方式wordpress 文件存储
  • 深圳宝安做网站公司wordpress edd插件
  • 南山做网站价格移动网站建设论文
  • 建设网站的发布与推广dw网页制作软件免费
  • 有口碑的大连网站建设网站的设计 更新
  • 优秀的吉祥物设计网站塑造什么品牌加快建设博物馆群