spring aop -- 獲取註解值
阿新 • • 發佈:2019-01-06
這兩天在寫安全框架
但是一直用method Invocation的話有點特別麻煩,看著不爽
用註解習慣了
現在一個需求就是
我要拿到這個a方法上的printLog註解的desc值
@PrintLog(desc = "i am a")
public void a(){
}
經過搜尋引擎來一遍之後
點選看官方文件,各種方式
發現了這種符合我需求的方法
為了省事 我定義了這個註解的方法為切點
@Before(“log()&&@annotation(printLog)”)就實現了我的需求
不得不驚歎aspect的語法
長征路漫漫 我還需努力
//定義一個切點
@Pointcut("@annotation(com.wuhulala.studySpring.annotation.PrintLog)")
public void log() {
}
@Before("log()&&@annotation(printLog)")
public void before(JoinPoint joinPoint, PrintLog printLog) {
loggger.debug(printLog.desc() + "[" + joinPoint.getSignature().getName() + " : start.....]" );
}
那麼如果需要兩個註解呢。。。。
&&表示與的意思 表示既有printLog註解 又有prinLog2註解 才會進入這個切面
具體可以瞅瞅
@annotation(printLog) 需要與引數列表中的那個引數名稱相同 如 PrintLog printLog
@Before("log()&&@annotation(printLog)&&@annotation(printLog2)")
public void before(JoinPoint joinPoint, PrintLog printLog, PrintLog2 printLog2) {
logger.debug("printLog[" + printLog.desc() + "]--printLog2[" + printLog2.desc() + "]");
// logger.debug(printLog.desc() + "[" + joinPoint.getSignature().getName() + " : start.....]");
// logger.debug(printLog2.desc() + "[" + joinPoint.getSignature().getName() + " : start.....]");
}
還可以這樣 如果需要特別多個 不嫌麻煩的話可以慢慢來。。。
還沒發現比較好點的方法。。。
慢慢來。。。
之前aspect瞭解的太少
看來 aspect 也要納入深入範圍了。。