我想在執行方法和方法結束時記錄日誌。爲此,我有一個簡單的方面用於記錄方法開始/結束的Java方面
@Aspect
public class StartEndLogger {
@Around("execution(* *(..)) && @annotation(StartEndLog)")
public Object around(ProceedingJoinPoint point) {
Object result = null;
System.out.println("start");
try {
result = point.proceed();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("end");
return result;
}
}
然後定義註解:
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
public @interface StartEndLog {
}
出於測試我有:
@StartEndLog
public static void main(String[] args) throws IOException {
}
事情是,該方面不攔截方法執行(運行時沒有進入方面)。我應該以某種方式註冊此註釋的方面嗎?我不使用任何Spring。
你見過這個嗎? http://www.yegor256.com/2014/06/01/aop-aspectj-java-method-logging.html – maksimov
如果註釋具有包名稱,則需要在註釋樣式切入點中使用完全限定名稱。如果您使用本地語法,則不需要,那麼您只需使用導入。 – kriegaex