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添加如 ...
随机推荐
- EditTextView
package com.egojit.android.sops.views.EditText; import android.content.Context; import android.graph ...
- Android无线测试之—UiAutomator UiSelector API介绍之一
一. UiSelector类介绍: 1) UiSelector类说明: UiSelector代表一种搜索条件,可以在当前界面上查询和获取特定元素的句柄,当找到多余一个的匹配元素,则返回布局层次结构上第 ...
- 关于org.apache.shiro.SecurityUtils.getSubject().getSession()
Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); s ...
- Ubuntu 16.04特性及使用基本方法
十招让Ubuntu 16.04用起来更得心应手 Ubuntu 16.04 LTS的这十项新功能,每个Ubuntu用户必须要知道! Ubuntu 16.04 LTS安装好需要设置的15件事
- 利用Hibernate注解生成表
转自:http://blog.csdn.net/madison__/article/details/55677099 Hibernate4注释 @Entity(name = "tbl_use ...
- MySQL权限系统(一).The MySQL Access Privilege System 概述
纯属个人阅读,如有翻译错误,请指出 The primary function of the MySQL privilege system is to authenticate a user who c ...
- Python中 sys.argv[]的用法实操
使用sys.argv[]首先要调用模块sys import sys 通俗说,这个东西就是取代了input的功能,input是用pycharm上,而argv[]是用在命令行上 也就是window 上的小 ...
- 前端框架之jQuery(二)----轮播图,放大镜
事件 页面载入 ready(fn) //当DOM载入就绪可以查询及操纵时绑定一个要执行的函数. $(document).ready(function(){}) -----------> ...
- wget: unable to resolve host address “http”
[root@one ~]# wget www.baidu.com --2017-09-24 10:20:23-- http://www.baidu.com/ Resolving http... fai ...
- 使用openresty + lua 搭建api 网关(一)安装openresty ,并添加lua模块
openresty 有点不多说,网上各种介绍,先安装吧. 官方操作在此,http://openresty.org/cn/installation.html, tar -xzvf openresty-V ...