AOP 提供了一種面向切面操作的擴充機制,通常這些操作是與業務無關的,在實際應用中,可以實作:日志處理、事務控制、參數校驗和自定義注解等功能。
一、日志處理
在調試程式時,如果需要在執行方法前列印方法參數,或者在執行方法後列印方法傳回結果,可以使用切面來實作。
@Slf4j
@Aspect
@Component
public class LoggerAspect {
@Around("execution(* cn.codeartist.spring.aop.sample.*.*(..))")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
// 方法執行前日志
log.info("Method args: {}", joinPoint.getArgs());
Object proceed = joinPoint.proceed();
// 方法執行後日志
log.info("Method result: {}", proceed);
return proceed;
}
}
二、事務控制
Spring 提供的聲明式事務也是基于 AOP 來實作的,在需要添加事務的方法上面使用 @Transactional 注解。
@Service
public class DemoService {
@Transactional(rollbackFor = Exception.class)
public void insertBatch() {
// 帶事務控制的業務操作
}
}
三、參數校驗
如果需要在方法執行前對方法參數進行校驗時,可以使用前置通知來擷取切入點方法的參數,然後進行校驗。
@Slf4j
@Aspect
@Component
public class ValidatorAspect {
@Before("execution(* cn.codeartist.spring.aop.sample.*.*(..))")
public void doBefore(JoinPoint joinPoint) {
// 方法執行前校驗參數
Object[] args = joinPoint.getArgs();
}
}
四、自定義注解
因為 AOP 可以攔截到切入點方法,Spring 也支援通過注解的方式來定義切點表達式,是以可以通過 AOP 來實作自定義注解的功能。
例如,自定義一個注解來實作聲明式緩存,把方法的傳回值進行緩存。
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Cacheable {
/**
* 緩的Key
*/
String key();
/**
* 緩存過期時間
*/
long timeout() default 0L;
/**
* 緩存過期時間機關(預設:毫秒)
*/
TimeUnit timeUnit() default TimeUnit.MILLISECONDS;
}
然後定義一個切片來實作正常的緩存操作,先讀緩存,緩存不存在時執行方法,然後把方法的傳回結果進行緩存。
@Aspect
@Component
public class AnnotationAspect {
@Around("@annotation(cacheable)")
public Object doAround(ProceedingJoinPoint joinPoint, Cacheable cacheable) throws Throwable {
// 自定義緩存邏輯
return joinPoint.proceed();
}
}
五、若依記錄檔處理
代碼目錄,代碼存放在ruoyi-framework子產品下面,具體目錄如下
package com.ruoyi.framework.aspectj;
1、如下圖代碼,日志攔截過程分為處理請求前執行、處理完請求後執行、攔截異常操作,其中正常處理和異常處理都會調用handleLog的方法插入記錄檔。
其中正常處理,參數送入jsonResult,響應結果參數
異常處理,參數送入e。記錄異常日志
/**
* 處理請求前執行
*/
@Before(value = "@annotation(controllerLog)")
public void boBefore(JoinPoint joinPoint, Log controllerLog)
{
TIME_THREADLOCAL.set(System.currentTimeMillis());
}
/**
* 處理完請求後執行
*
* @param joinPoint 切點
*/
@AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
public void doAfterReturning(JoinPoint joinPoint, Log controllerLog, Object jsonResult)
{
handleLog(joinPoint, controllerLog, null, jsonResult);
}
/**
* 攔截異常操作
*
* @param joinPoint 切點
* @param e 異常
*/
@AfterThrowing(value = "@annotation(controllerLog)", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Log controllerLog, Exception e)
{
handleLog(joinPoint, controllerLog, e, null);
}
如下是記錄檔插入代碼
protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult)
{
try
{
// 擷取目前的使用者
LoginUser loginUser = SecurityUtils.getLoginUser();
// *========資料庫日志=========*//
SysOperLog operLog = new SysOperLog();
operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
// 請求的位址
String ip = IpUtils.getIpAddr();
operLog.setOperIp(ip);
operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255));
if (loginUser != null)
{
operLog.setOperName(loginUser.getUsername());
}
if (e != null)
{
operLog.setStatus(BusinessStatus.FAIL.ordinal());
operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
}
// 設定方法名稱
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
operLog.setMethod(className + "." + methodName + "()");
// 設定請求方式
operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
// 處理設定注解上的參數
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
// 設定消耗時間
operLog.setCostTime(System.currentTimeMillis() - TIME_THREADLOCAL.get());
// 儲存資料庫
AsyncManager.me().execute(AsyncFactory.recordOper(operLog));
}
catch (Exception exp)
{
// 記錄本地異常日志
log.error("異常資訊:{}", exp.getMessage());
exp.printStackTrace();
}
finally
{
TIME_THREADLOCAL.remove();
}
}
業務邏輯:
1、擷取登入使用者資訊。
2、建立SysOperLog實列對像,初始化對象值。
3、記錄ip位址(IpUtils.getIpAddr())、設定狀态是成功還是失敗、設定消耗時間。
4、通過異步插入資料庫AsyncManager.me().execute(AsyncFactory.recordOper(operLog));
5、異步插入可以減少接口響應時間。