使用springMVC AOP实现日记记录
来源:互联网 发布:高洛峰php 编辑:程序博客网 时间:2024/06/10 22:16
1 . 配置springmvc驱动,以及包扫描,还有AOP配置,如下:
<context:component-scan base-package="org.lee" /> <mvc:annotation-driven /><!-- 注解实现日记记录 --> <aop:aspectj-autoproxy />
2 . 引入相关jar包
3 . 目录树
4 . 编写日记记录类 LogInterceptor.java
package org.lee.Interceptor;import java.lang.reflect.Method;import java.util.Arrays;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;@Component@Aspectpublic class LogInterceptor { /** * 这里针对org.lee.service.Imp目录下的新增,修改方法进行记录 * */ @Pointcut("execution(* org.lee.service.Imp.*.*create*(..)) || execution(* org.lee.service.Imp.*.*edit*(..))")public void allMethod(){} @Before("allMethod()") public void before(){ }@After("allMethod()")public void afterLog(JoinPoint point){System.out.println("--------------afterLog:--------------最终通知");System.out.println("--------------afterLog:=========目标方法为:"+point.getSignature().getDeclaringTypeName()+"."+point.getSignature().getName());System.out.println("--------------afterLog:--------------参数为:"+Arrays.toString(point.getArgs()));System.out.println("--------------afterLog:--------------被织入的对象为:"+point.getTarget());}@AfterReturning(value="allMethod()",returning="returnValue")public void afterRunningLog(JoinPoint point,Object returnValue ){System.out.println("--------------afterRunningLog--------------返回值后通知");System.out.println("--------------afterRunningLog----------------目标方法:"+point.getSignature().getDeclaringTypeName()+"."+point.getSignature().getName());System.out.println("--------------afterRunningLog----------------参数为:"+Arrays.toString(point.getArgs()));System.out.println("--------------afterRunningLog----------------返回值:"+returnValue); }@AfterThrowing(value="allMethod()",throwing="ex")public void AfterThrowingLog(Throwable ex){System.out.println("--------------AfterThrowingLog--------------进入异常通知");System.out.println("--------------AfterThrowingLog--------------异常信息:"+ex.getMessage());}@Around("allMethod()")public Object doAround(ProceedingJoinPoint p) throws Throwable{System.out.println("--------------Around--------------进入环绕通知"); long startTime = System.currentTimeMillis(); Object obj = p.proceed(); long endTime = System.currentTimeMillis(); MethodSignature signature = (MethodSignature) p.getSignature(); String methodName = signature.getDeclaringTypeName() + "." + signature.getName(); System.out.println("目标方法:"+methodName); System.out.println("运行耗时:"+(endTime-startTime)+"ms");System.out.println("--------------Around--------------结束方法调用");return obj;}}
测试:
0 0
- 使用springMVC AOP实现日记记录
- springmvc中使用aop实现登录验证
- SpringMVC利用AOP实现自定义注解记录日志
- SpringMVC利用AOP实现自定义注解记录日志
- SpringMVC利用AOP实现自定义注解记录日志
- SpringMVC利用AOP实现自定义注解记录日志
- springMVC自定义注解,用AOP来实现日志记录
- SpringMVC利用AOP实现自定义注解记录日志
- springMVC +Mybatis +spring aop 实现用户系统操作日志记录
- SpringMVC利用AOP实现自定义注解记录日志
- SpringMVC利用AOP实现自定义注解记录日志
- SpringMVC利用AOP实现自定义注解记录日志
- SpringMVC中使用AOP
- SpringMVC 使用 AOP注解
- 日记记录工具使用
- spring2.x使用aop实现声明式日志记录
- 使用Spring的AOP实现接口方法执行时间记录
- Spring + Aop+注解 集成使用 Log4j,实现异常日志记录
- nginx做负载均衡的几种方式
- 纯css实现n个导航栏均分效果
- 16进制和八个二进制位的关系
- 使用poi统计工作职责
- App 模块化实战经验总结
- 使用springMVC AOP实现日记记录
- Spring 中关于@Resource注解,以及其和@Autowired注解的区别
- python获取当前工作目录
- Python 基础教程(好网站)——001
- 二维码-clipboard的使用
- 大数据:Spark Shuffle(二)Executor、Driver之间Shuffle结果消息传递、追踪
- FTS数据库优化(Android)原理与应用详解(1)
- LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- 大三学前端第二阶段--js第一部分总结