springboot---aop切片编程
1.介绍
面向切面编程,关注点代码与业务代码分离,就是给指定方法执行前执行后。。插入重复代码
关注点:重复代码
切面:被切面的类
切入点:执行目标对象方法,动态植入切片代码
2.部署步骤
2.1:添加依赖
<!-- spring aop支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
2.2 aop切片配置
package com.ty.tyzxtj.aop; import java.util.HashMap;
import java.util.Map;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute;
import javassist.bytecode.MethodInfo; /**
* 调用接口的切面类
* @author wangjiping
*
*/
@Aspect
@Component
public class WebLogAspect {
private Logger logger = null;
/**
* 定义一个切面
*/
@Pointcut("execution(* com.ty.tyzxtj.controller.*.*(..))")
public void webLog(){ }
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
Class<?> target = joinPoint.getTarget().getClass();//目标类
logger = LoggerFactory.getLogger(target);//对应日志成员
String clazzName = target.getName();//类名
Object[] args = joinPoint.getArgs();//参数
String methodName = joinPoint.getSignature().getName();//方法名
Map<String,Object> nameAndArgs = getFieldsName(this.getClass(), clazzName, methodName, args);//获取被切参数名称及参数值
logger.info("["+clazzName+"]["+methodName+"]["+nameAndArgs.toString()+"]");
//获取参数名称和值
} @AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容
logger.info("response : " + ret);
} /**
* 通过反射机制 获取被切参数名以及参数值
*
* @param cls
* @param clazzName
* @param methodName
* @param args
* @return
* @throws NotFoundException
*/
private Map<String, Object> getFieldsName(Class<?> cls, String clazzName, String methodName, Object[] args) throws NotFoundException {
Map<String, Object> map = new HashMap<String, Object>();
ClassPool pool = ClassPool.getDefault();
//ClassClassPath classPath = new ClassClassPath(this.getClass());
ClassClassPath classPath = new ClassClassPath(cls);
pool.insertClassPath(classPath); CtClass cc = pool.get(clazzName);
CtMethod cm = cc.getDeclaredMethod(methodName);
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
}
int pos = Modifier.isStatic(cm.getModifiers()) ? : ;
for (int i = ; i < cm.getParameterTypes().length; i++) {
map.put(attr.variableName(i + pos), args[i]);//paramNames即参数名
}
return map;
}
}
3.注意事项
springboot---aop切片编程的更多相关文章
- springBoot AOP切面编程
AOP 为 Aspect Oriented Programming 的缩写,意为 面向切面编程.AOP 为spring 中的一个重要内容,它是通过对既有程序定义一个切入点,然后在其前后切入不同的执行内 ...
- 基于SpringBoot AOP面向切面编程实现Redis分布式锁
基于SpringBoot AOP面向切面编程实现Redis分布式锁 基于SpringBoot AOP面向切面编程实现Redis分布式锁 基于SpringBoot AOP面向切面编程实现Redis分布式 ...
- springBoot AOP学习(一)
AOP学习(一) 1.简介 AOp:面向切面编程,相对于OOP面向对象编程. Spring的AOP的存在目的是为了解耦.AOP可以让一切类共享相同的行为.在OOP中只能通过继承类或者实现接口,使代码的 ...
- Spring:面向切片编程
在之前我们记录Spring的随笔当中,都是记录的Spring如何对对象进行注入,如何对对象的属性值进行注入,即我们讲解的很大部分都是Spring的其中一个核心概念——依赖注入(或者说是控制翻转,IOC ...
- 十:SpringBoot-配置AOP切面编程,解决日志记录业务
SpringBoot-配置AOP切面编程,解决日志记录业务 1.AOP切面编程 1.1 AOP编程特点 1.2 AOP中术语和图解 2.SpringBoot整合AOP 2.1 核心依赖 2.2 编写日 ...
- SpringBoot AOP中JoinPoint的用法和通知切点表达式
前言 上一篇文章讲解了springboot aop 初步完整的使用和整合 这一篇讲解他的接口方法和类 JoinPoint和ProceedingJoinPoint对象 JoinPoint对象封装了Spr ...
- 为了支持AOP的编程模式,我为.NET Core写了一个轻量级的Interception框架[开源]
ASP.NET Core具有一个以ServiceCollection和ServiceProvider为核心的依赖注入框架,虽然这只是一个很轻量级的框架,但是在大部分情况下能够满足我们的需要.不过我觉得 ...
- spring6——AOP的编程术语
面向切面编程作为一种编程思想,允许我们对程序的执行流程及执行结果动态的做出改变,以达到业务逻辑之间的分层管理或者是目标对象方法的增强,spring框架很好的实现了这种编程思想,让我们可以对主业务逻辑和 ...
- springboot+aop切点记录请求和响应信息
本篇主要分享的是springboot中结合aop方式来记录请求参数和响应的数据信息:这里主要讲解两种切入点方式,一种方法切入,一种注解切入:首先创建个springboot测试工程并通过maven添加如 ...
随机推荐
- for循环的执行循序
先上一段代码,大家说出此方法的执行结果: public class Print{ static boolean out(char c){ System.out.println(c); return t ...
- 64位matlab mex64位编译器解决方案
安装libsvm的时候用到了mex -setup,有的会报 Could not find the 64-bit compiler. This may indicate that the "X ...
- FW ImageMagick
ExploitFixes ImageMagick < 6.9.3-9 - Multiple Vulnerabilities 2016-05-04 22:05:53 Nikolay Ermishk ...
- 【转】jstack简单使用
转载记录下, 转载自https://www.cnblogs.com/chenpi/p/5377445.html 当我们运行java程序时,发现程序不动,但又不知道是哪里出问题时,可以使用JDK自带的j ...
- IBM WebSphere cannot start in RAD 9.1
Have solved the problem. Solutions follows Step 1: double click on "WebSphere Application Serve ...
- Linux中的环境变量配置文件及其作用
登录相关的配置文件: /etc/profile 范围:对所有用户生效 作用: a.定义USER变量 b.定义LOGNAME变量 c.定义MAIL变量 d.定义PATH变量 e.定义HOSTNAME变量 ...
- Android studio 相关下载
Android studio http://www.androiddevtools.cn/ Oracle的VirtulBox https://www.virtualbox.org/wiki/Down ...
- 我的第四个Python小程序
简单的登陆程序: # Author: fansik # Description: Simply log in to small programs # Date: 2017/9/29 _username ...
- python中的逻辑操作符
python中主要有三个逻辑操作符,分别是:and.or.not. and:且,所有人为真才为真. or:或,一个为正就是真. not:非,取反. >>> print(3>2 ...
- 极速Node.js:来自LinkedIn的10个性能提升秘籍
from:http://faylai.iteye.com/blog/1293194 1.避免使用同步的方法 nodejs 是基于单线程.为了让单线程能够处理高并发的请求,我们尽量要避免让线程等待,阻塞 ...