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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!