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. Netty中的责任链模式

    适用场景: 对于一个请求来说,如果有个对象都有机会处理它,而且不明确到底是哪个对象会处理请求时,我们可以考虑使用责任链模式实现它,让请求从链的头部往后移动,直到链上的一个节点成功处理了它为止 优点: ...

  2. C#多线程学习之如何操纵一个线程

    下面我们就动手来创建一个线程,使用Thread类创建线程时,只需提供线程入口即可.(线程入口使程序知道该让这个线程干什么事) 在C#中,线程入口是通过ThreadStart代理(delegate)来提 ...

  3. SQL数据同步到ElasticSearch(三)- 使用Logstash+LastModifyTime同步数据

    在系列开篇,我提到了四种将SQL SERVER数据同步到ES中的方案,本文将采用最简单的一种方案,即使用LastModifyTime来追踪DB中在最近一段时间发生了变更的数据. 安装Java 安装部分 ...

  4. 基于ReentrantLock的非公平锁理解AQS

    AQS AQS概述 ​ AbstractQueuedSynchronizer抽象队列同步器简称AQS,它是实现同步器的基础组件,juc下面Lock的实现以及一些并发工具类就是通过AQS来实现的,这里我 ...

  5. TCP传输协议如何进行拥塞控制?

    拥塞控制 拥塞现象是指到达通信子网中某一部分的分组数量过多,使得该部分网络来不及处理,以致引起这部分乃至整个网络性能下降的现象,严重时甚至会导致网络通信业务陷入停顿,即出现死锁现象.这种现象跟公路网中 ...

  6. 【iOS】ERROR ITMS-90032: "Invalid Image Path...

    用 Application Loader 提交苹果审核时出现了这个问题,具体如下: ERROR ITMS-: "Invalid Image Path - No image found at ...

  7. ubuntu .deb .tar.gz .tar.bz2 .rmp 和命令方式安装软件的方法

    今天在Ubuntu11.10中安装Google chrome浏览器是遇到了问题,下载好的".deb"格式的安装文件google-chrome-stable.deb双击后或者右键快捷 ...

  8. HttpClientFactory 使用说明 及 对 HttpClient 的回顾和对比

    目录 HttpClient 日常使用及坑点: HttpClientFactory 优势: HttpClientFactory 使用方法: 实战用法1:常规用法 1. 在 Startup.cs 中进行注 ...

  9. git和githup

    一:Git简介 1.1:VCS的历史 Git是一款代码管理工具(Version Control System),傲视群雄,是目前世界上最先进的免费开源的分布式版本控制系统,没有之一! VCS版本控制系 ...

  10. ajax具体实现学习记录

    记录自己对ajax\的理解, 首先要明白ajax是为了解决什么问题,简单来讲就是为了局部刷新页面,而不刷新整个界面.就比如现在有一个实时热度的显示,它是不断变化的,所以你肯定要不停的从数据库当中获取热 ...