1.首先實現AOP實例的第一步即聲明切面類,兩種方式(1.基於注解形式@Aspect,2.基於xml配置,一般都通過注解來聲明切面類)
2.切入點表達式大致也有兩種,一種是直接根據方法的簽名來匹配各種方法@Pointcut("execution(xxxxxx表達式)"),另一種即標題的通過自定義注解的形式@Pointcut("@annotation(注解名)")
3.首先自定義注解
@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ExtendPoint { }
這里順便記錄java中四大元注解的作用:
@Target 表示該注解用於什么地方,可能的值在枚舉類 ElemenetType 中,包括:
ElemenetType.CONSTRUCTOR----------------------------構造器聲明
ElemenetType.FIELD --------------------------------------域聲明(包括 enum 實例)
ElemenetType.LOCAL_VARIABLE------------------------- 局部變量聲明
ElemenetType.METHOD ----------------------------------方法聲明
ElemenetType.PACKAGE --------------------------------- 包聲明
ElemenetType.PARAMETER ------------------------------參數聲明
ElemenetType.TYPE--------------------------------------- 類,接口(包括注解類型)或enum聲明
@Retention 表示在什么級別保存該注解信息。可選的參數值在枚舉類型 RetentionPolicy 中,包括:
RetentionPolicy.SOURCE ---------------------------------注解將被編譯器丟棄
RetentionPolicy.CLASS -----------------------------------注解在class文件中可用,但會被VM丟棄
RetentionPolicy.RUNTIME VM-------將在運行期也保留注釋,因此可以通過反射機制讀取注解的信息。
@Documented 將此注解包含在 javadoc 中 ,它代表着此注解會被javadoc工具提取成文檔。在doc文檔中的內容會因為此注解的信息內容不同而不同。相當與@see,@param 等。
@Inherited 允許子類繼承父類中的注解。
4.聲明切面類
@Aspect @Component public class ExtendAspect { @Pointcut("@annotation(ExtendPoint)") private void aspectjMethod(){}; @Before("aspectjMethod()") public void before(JoinPoint jp, ExtendPoint ep){ } @Around(value = "aspectjMethod()") public Object around(ProceedingJoinPoint point) throws Throwable { Object[] args = point.getArgs(); //用改變后的參數執行目標方法 Object returnValue = point.proceed(args); return returnValue; } @AfterReturning(value = "aspectjMethod()", returning = "returnValue") public void log(JoinPoint point, Object returnValue){ } @After(value = "aspectjMethod()") public void after(JoinPoint point){ } @AfterThrowing(value = "aspectjMethod()", throwing = "ex") public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) { } }
