AspectJ是一个AOP框架,由于SpringAOP的配置过于繁琐,因此使用了AspectJ依赖注解开发

1、Aspecj依赖坐标,此处省略了Spring相关依赖

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.1</version>
</dependency>
2、在applicationContext.xml文件中配上

<aop:aspectj-autoproxy/>
 代表使用Aspecj的注解开发模式

3、自定义创建的切入点,以及要实现的功能

package com.util;

import com.dao.LogDAO;

import com.entity.LogMessage;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.UUID;
@Aspect // 用来声明这是一个AspectJ
@Component // 在此处声明一个Component 是因为Spring扫描注解并不能识别AspectJ 因此在此处声明,不必在applicationContext.xml配置bean标签了
public class ServiceLog {

@Autowired
private LogDAO logDAO;
//在该无参无内容的方法上添加一个@Poincut()来声明切入点,在后来的@Around("pc()")直接写入该方法的名字就能在自动使用这些切点
@Pointcut("execution(* com.service..*.add*(..)) || execution(* com.service..*.modify*(..)) || execution(* com.service..*.drop*(..))")
public void pc() {}
//环绕通知注解
@Around("pc()")
public Object log(ProceedingJoinPoint pjp) throws Throwable {
// 获取request
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

// 获取Session
HttpSession session = request.getSession();

// 获取用户名
String adminName = (String) session.getAttribute("adminName");

// UUID
String uuidName = UUID.randomUUID().toString().replace("-","");

LogMessage log = new LogMessage();

log.setLogId(uuidName);
log.setLogUser(adminName);
log.setLogTime(new Date());

String totalArg = null;
//获取参数
Object[] args = pjp.getArgs();
for (Object arg : args) {
totalArg += arg;
totalArg += " ";
}
log.setLogMessage(totalArg);
// 返回目标方法的签名
MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
// 获取方法对象
Method method = methodSignature.getMethod();
// 获取方法名称
String methodName = method.getName();

String firstCode = methodName.substring(0,1);

if (firstCode.equals("a")) {
log.setLogAction("添加");
} else if (firstCode.equals("m")) {
log.setLogAction("修改");
} else {
log.setLogAction("删除");
}
// 获取方法所在的类 及类型
// System.out.println(methodSignature.getDeclaringType());
// 获取方法所在的类
String oldName = methodSignature.getDeclaringTypeName();
String suffix = oldName.substring(oldName.lastIndexOf(".") + 1);
log.setLogResource(suffix);
// // 获取方法的注解
// Annotation[] annotation = method.getAnnotations();
Object obj = null;
try {
obj = pjp.proceed();
log.setLogResult("success");
} catch (Exception e) {
e.printStackTrace();
log.setLogResult("error");
}
logDAO.inserLog(log);
return obj;
}
}
该类实现的是用户操作后台的日志记录,实现了用户在进行增删改时记录用户信息,操作的类以及方法参数。

这样一个简单的AspectJ就实现了。

AspactJ框架常用注解

@PonitCut  // 声明切入点的注解
@Before    // 声明前置通知注解
@After     // 声明后置通知注解(原始方法执行正常或者非正常执行都会进入)
@AfterReturing // 声明后置通知注解(原始方法必须正常执行)
@AfterThrowing // 声明异常通知
@Around // 环绕通知注解
---------------------
作者:吾生
来源:CSDN
原文:https://blog.csdn.net/qq_41181619/article/details/81021680
版权声明:本文为博主原创文章,转载请附上博文链接!

简单使用AspectJ的更多相关文章

  1. spring-第十五篇之AOP面向切面编程之AspectJ框架简单应用

    1.去官方网站下载aspectj-1.8.0.jar 2.在jar包目录启动cmd,执行java -jar aspectj-1.8.0.jar,Next 3.检查JAVA_HOME路径是否正确,如果不 ...

  2. Spring3系列12- Spring AOP AspectJ

    Spring3系列12- Spring AOP AspectJ 本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代 ...

  3. Spring框架(6)---AspectJ实现AOP

    AspectJ实现AOP 上一篇文章Spring框架(4)---AOP讲解铺垫,讲了一些基础AOP理解性的东西,那么这篇文章真正开始讲解AOP 通过AspectJ实现AOP要比普通的实现Aop要方便的 ...

  4. Spring AOP AspectJ

    本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代码. Pointcut:注入Advice的位置,切入点,一般为某 ...

  5. Spring结合AspectJ的研究

    本文阐述以下内容:1.AspectJ是什么及使用方式2.Spring AOP和AspectJ的区别3.Spring结合AspectJ的使用方法和原理4.Spring注解方式使用AspectJ遇到的问题 ...

  6. Spring MVC系列-(5) AOP

    5 AOP 5.1 什么是AOP AOP(Aspect-Oriented Programming,面向切面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的 ...

  7. AspectJ的简单使用

    aspectj是一款优秀的面向切面的编程框架,下面就简单介绍一下入门教程吧: 1.官网下载AspectJ的jar包,我这里下的是最新版本1.8.7的. 2.因为AspectJ.jar 是一个可执行的j ...

  8. 简单直白的去理解AOP,了解Spring AOP,使用 @AspectJ - 读书笔记

    AOP = Aspect Oriental Programing  面向切面编程 文章里不讲AOP术语,什么连接点.切点.切面什么的,这玩意太绕,记不住也罢.旨在以简单.直白的方式理解AOP,理解Sp ...

  9. Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现

    转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...

随机推荐

  1. .net持续集成sonarqube篇之sonarqube基本操作(二)

    系列目录 Activity界面操作 Activity界面主要是对多次构建管理界面,主要是帮助管理员快速了解项目每次构建与以往构建相比问题是增加了还是减少了等指标.由于目前我们仅进行了一次构建,因此没有 ...

  2. JS+Jquery自定义格式导出HTML为Word(下列插件同样可以用于Excel导出)

    这里的word导出主要采用了jquery.wordexport.js.FileSaver.js,做功能之前我也是找了很多网上的资料,里面涉及到js导出word的用的都是这个插件,只是在自定义样式这一块 ...

  3. 六、SQL 多张表数据叠加到一个视图里面

    1 create view vABC as select * from a,b,c where a.id = b.aid and b.id = c.bid ---------------------- ...

  4. phpStudy 升级 MySQL 到 5.7.21

    1.备份原来的MySQL 我的路径是D:\phpStudy2018\PHPTutorial\MySQL\bin 修改文件名为MySQL-backup 2.下载新的MySQL 5.7.21 网址:htt ...

  5. JS中构造函数和普通函数有什么区别

    JS中构造函数有普通函数有什么区别? 1.一般规则 构造函数都应该以 一个大写字母开头,eg: function Person(){...} 而非构造函数则应该以一个小写字母开头,eg: functi ...

  6. 【iOS】ARC & MRC

    iOS 项目类型,是 ARC 还是 MRC 未完……

  7. 【Spring源码解析】—— 委派模式的理解和使用

    一.什么是委派模式 委派模式,是指什么呢?从字面含义理解,委派就是委托安排的意思,委派模式就是在做具体某件事情的过程中,交给其他人来做,这个事件就是在我的完整链路上的一部分,但是复杂度较高的情况下或者 ...

  8. java常见面试题目(三)

    1.jsp的内置对象. JSP中一共预先定义了9个这样的对象,分别为:request.response.session.application.out.pagecontext.config.page. ...

  9. java并发编程(十四)----(JUC原子类)对象的属性修改类型介绍

    今天我们介绍原子类的最后一个类型--对象的属性修改类型: AtomicIntegerFieldUpdater,AtomicLongFieldUpdater,AtomicReferenceFieldUp ...

  10. Vue系列:Slot 插槽的使用范例

    插槽对于自定义的组件开发来说,是十分强大的功能.这篇主要做个简单梳理 插槽可以分3种: 1.简单插槽 2.具名插槽 3.作用域插槽