aop log切面
@Aspect:描述一个切面类,定义切面类的时候需要打上这个注解
@Component:spring-boot配置类
package com.*.*.tools; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest;
import java.util.Arrays; @Aspect
@Component
public class LogAspect { /**
* 功能描述: 拦截对这个包下所有方法的访问
*
* @param:[]
* @return:void
**/
@Pointcut("execution(* com.*.*.controller.*..*(..))")
public void loginLog() {
} // 前置通知
@Before("loginLog()")
public void loginBefore(JoinPoint joinPoint) { // 我们从请求的上下文中获取request,记录请求的内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); System.out.println("请求路径 : " + request.getRequestURL());
System.out.println("请求方式 : " + request.getMethod());
System.out.println("方法名 : " + joinPoint.getSignature().getName());
System.out.println("类路径 : " + joinPoint.getSignature().getDeclaringTypeName());
System.out.println("参数 : " + Arrays.toString(joinPoint.getArgs()));
} // @AfterReturning(returning = "object", pointcut = "loginLog()")
// public void doAfterReturning(Object object) {
//
// System.out.println("方法的返回值 : " + object);
// } // 方法发生异常时执行该方法
@AfterThrowing(throwing = "e",pointcut = "loginLog()")
public void throwsExecute(JoinPoint joinPoint, Exception e) { System.err.println("方法执行异常 : " + e.getMessage());
} // 后置通知
// @After("loginLog()")
// public void afterInform() {
//
// System.out.println("后置通知结束");
// } // 环绕通知
@Around("loginLog()")
public Object surroundInform(ProceedingJoinPoint proceedingJoinPoint) { long startTime=System.currentTimeMillis();
try {
Object o = proceedingJoinPoint.proceed();
long times=System.currentTimeMillis()-startTime;
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
String methodName = signature.getDeclaringTypeName() + "." + signature.getName();
System.out.println("【"+methodName+"】执行时间:" +times/1000+"s" );
return o;
} catch (Throwable e) {
e.printStackTrace();
return null;
}
}
}
aop log切面的更多相关文章
- Javascript aop(面向切面编程)之around(环绕)
Aop又叫面向切面编程,其中“通知”是切面的具体实现,分为before(前置通知).after(后置通知).around(环绕通知),用过spring的同学肯定对它非常熟悉,而在js中,AOP是一个被 ...
- 【原创】Android AOP面向切面编程AspectJ
一.背景: 在项目开发中,对 App 客户端重构后,发现用于统计用户行为的友盟统计代码和用户行为日志记录代码分散在各业务模块中,比如在视频模块,要想实现对用户对监控点的实时预览和远程回放行为进行统计, ...
- 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存
代码已上传Github+Gitee,文末有地址 上回<从壹开始前后端分离[ .NET Core2.0 Api + Vue 2.0 + AOP + 分布式]框架之九 || 依赖注入IoC学习 + ...
- javascript AOP(面向切面编程)
var func = function () { console.log("2") } Function.prototype.before = function (beforefn ...
- Spring Boot2(六):使用Spring Boot整合AOP面向切面编程
一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop aop全称Aspec ...
- 谈一谈AOP面向切面编程
AOP是什么 : AOP面向切面编程他是一种编程思想,是指在程序运行期间,将某段代码动态的切入到指定方法的指定位置,将这种编程方式称为面向切面编程 AOP使用场景 : 日志 事务 使用AOP的好处是: ...
- Z从壹开始前后端分离【 .NET Core2.0/3.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存
本文梯子 本文3.0版本文章 代码已上传Github+Gitee,文末有地址 大神反馈: 零.今天完成的深红色部分 一.AOP 之 实现日志记录(服务层) 1.定义服务接口与实现类 2.在API层中添 ...
- spring总结————AOP面向切面总结
spring总结————AOP面向切面 一.spring aop概念 spring aop面向切面编程,java是面向对象的语言. 真正的service层代码 业务逻辑层再处理业务之前和之后都要进行一 ...
- aop面向切面编程的实现
aop主要用于日志记录,跟踪,优化和监控 下面是来自慕课网学习的一些案例,复制黏贴就完事了,注意类和方法的位置 pom添加依赖: <dependency> <groupId>o ...
随机推荐
- redis入门笔记
redis入门笔记 参考redis实战手册 1. Redis在windows下安装 下载地址:https://github.com/MSOpenTech/redis/tags 安装Redis 1.1. ...
- spark学习12(Wordcount程序之spark-shell)
在目录/home/hadoop/2016113012下有文件words.txt hello scala hello java hello python hello wujiadong 上传该文件到hd ...
- HDU 1159 Common Subsequence 动态规划
2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...
- golang learning
开发用py和go 入职前学了几天py入职后看的代码也是py 现在终于还是要学go了 初体验:感觉和c py都很像 入门语法看起来很简单的样子 学了py之后现在各种随意 需要多注意 在函数传参的时候 c ...
- Valid sudoku, 是否是有效的数独
问题描述:给定9x9矩阵,看是是否是有效数独,不用全部都填上数字,可以为. 算法分析:这道题就是判断,不难,有效数独三个充分条件,行,列,3*3子矩阵,都要满足数字不能重复. public boole ...
- Generate parentheses,生成括号对,递归,深度优先搜索。
问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...
- 基础的JavaScript函数
基础的JavaScript函数 1.首字母大写 2.去除数组重复项 3.数组的排序 4.闭包 1. 把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字(使用JavaScript的map函 ...
- Oracle操作ORA-02289: 序列不存在
解决方案:实现创建序列,创建语句如下所示: create sequence employees_seq minvalue maxvalue start increment cache ; 这时候再执行 ...
- 发现project项目打红色感叹号的一种解决方案
在window-preferences中的xml catalog中对打叉的dtd进行remove. 很有可能是包导错了,打开config build path然后将打红叉的文件进行remove.
- 负载均衡之DNS域名解析
转载请说明出处:http://blog.csdn.net/cywosp/article/details/38017027 在上一篇文章(http://blog.csdn.net/cywosp/arti ...