首先敘述一下業務,在每個模組中達到某要求時都要給當前使用者新增積分,所以這裡用到了註解搭配aop。
首先自定義乙個註解
/**
* @author zhanggx
* @date 2021-01-06 16:40
*/@target()
@retention(retentionpolicy.runtime)
@documented
public @inte***ce addpoint
/**
* @author zhanggx
* @date 2021-01-06 16:41
*/@aspect
@component
public class addpointaspect
@around("aspect()")
public void addpoint(proceedingjoinpoint point) throws throwable else
}/**
* 異常通知 用於攔截記錄異常日誌
** @param joinpoint
* @param e
*/@afterthrowing(pointcut = "aspect()", throwing = "e")
public void doafterthrowing(joinpoint joinpoint, throwable e) catch (exception ex) ", ex.getmessage());
}/***********記錄本地異常日誌***********/
logger.error("異常方法:{}異常**:{}異常資訊:{}引數:{}", joinpoint.gettarget().getclass().getname() + joinpoint.getsignature().getname(), e.getclass().getname(), e.getmessage());
}}
/**
* 這是呼叫者的業務模組 這裡讓執行緒休眠了5秒 為了判斷是呼叫者先執行 還是切入點先執行
* @throws interruptedexception
*/@addpoint(add = true)
public void addpoint() throws interruptedexception
AOP 自定義註解
compile org.springframework.boot spring boot starter aop aspect component public class auditaspect before weblog public void debefore joinpoint joinpo...
自定義註解AOP
專案中有些模組需要加入工作流審批流程,並且可以選擇是否開啟工作流。於是根據需求就想到了用aop做乙個通用的模組開關檢驗。如果模組沒有開啟工作流,就直接放行。如果開啟了工作流,就啟動該模組的工作流流程。下面上 target elementtype.method retention retentionp...
通過 aop 寫 自定義註解
target elementtype.method retention retentionpolicy.runtime public inte ce iface1 target target說明了annotation所修飾的物件範圍 annotation可被用於 packages types 類 介...