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

AOP简单使用模版

AOP面向切面编程

切面类的定义之模版

package com.xie.service;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/*** 切面演示 包括切点定义 各通知定义*/
@Aspect
@Component
public class AopService {/*** 定义切点* com.xie.controller.*.*(..) 表示 controller包下 的所有类 的所有方法(*(..))*/@Pointcut("execution(* com.xie.controller.*.*(..))")private void checkMem() {System.out.println("*************** 切点 *****************");}/*** 前置此切点? 切面? 通知 已绑定 切点*/@Before("checkMem()")private void before(JoinPoint joinPoint) {HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();System.out.println("----------------1----------------");System.out.println("前置通知");System.out.println("Url is:" + request.getRequestURL().toString());System.out.println("method is:" + joinPoint.getSignature().getName());System.out.println("----------------1----------------");}/*** 后置此切点? 切面? 通知 已绑定 切点*/@After("checkMem()")private void printMem() {System.out.println("---------------2-----------------");System.out.println("后置通知");System.out.println("After the method, is Mem usage is:" + Runtime.getRuntime().freeMemory() / 1024 / 1024 + "M");System.out.println("----------------2----------------");}/*** 环绕此切点? 切面? 通知 已绑定 切点*/@Around("checkMem()")private Object around(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("---------------3-----------------");System.out.println("环绕通知");System.out.println("Around for AOP");//获取方法参数值数组Object[] args = joinPoint.getArgs();Object ret = joinPoint.proceed(args);System.out.println("proceed args, result is: " + ret);System.out.println("----------------3----------------");//调用方法return ret;}/*** 后置成功通知  切面? 通知 已绑定 切点*/@AfterReturning(pointcut = "checkMem()", returning = "returnObj")private void afterReturning(Object returnObj) {System.out.println("@AfterReturning 4 ========后置返回(成功)通知===========");System.out.println("--------------------------------");System.out.println("return value is:" + returnObj);System.out.println("--------------------------------");}/*** 后置异常通知  切面? 通知 已绑定 切点*/@AfterThrowing(pointcut = "checkMem()", throwing = "e")private void afterThrowing(JoinPoint joinPoint, Exception e) {System.out.println("@AfterThrowing 5 =====后置异常通知==============");System.out.println("--------------------------------");System.out.println("Exception is:" + e.getMessage());System.out.println("--------------------------------");}
}
http://www.lryc.cn/news/207755.html

相关文章:

  • 手机注册.
  • PostgreSQL 17新特性之登录事件触发器
  • Docker 搭建 LNMP + Wordpress
  • 大数据调度最佳实践 | 从Airflow迁移到Apache DolphinScheduler
  • node实战——搭建带swagger接口文档的后端koa项目(node后端就业储备知识)
  • Qt篇——子控件QLayoutItem与实际控件的强转
  • Css3使用
  • 【嵌入式开源库】timeslice的使用,完全解耦的时间片轮询框架构
  • 人工智能期末考试(刷题篇部分题有答案)
  • 手写Vue渲染器render函数
  • CGAL+QT
  • GBase8a SSL 配置
  • 数据结构之队列(源代码➕图解➕习题)
  • 社区迭代|ETLCloud社区新增“论坛”啦!
  • ohos的代码同步以及添加自己的代码
  • Python的Pandas库(二)进阶使用
  • 如何才能从程序员到架构师?
  • dvadmin-打包发布-nginx-静态服务器配置-防火墙设置
  • Win10中Pro/E鼠标滚轮不能缩放该怎么办?
  • 腾讯云轻量应用服务器性能如何?值得入手吗?
  • 主流大语言模型的技术细节
  • 面试经典150题——Day22
  • for循环三种跳出循环的方法(retrun、continue、break)
  • React中的受控组件(controlled component)和非受控组件(uncontrolled component)
  • python 查找波峰和波谷
  • 深入理解 Document Load 和 Document Ready 的区别
  • 有趣的算法(七) ——快速排序改进算法
  • Vue3 + Tsx 集成 ace-editor编辑器
  • TypeScritpt中的namespace
  • LeetCode75——Day17