一、切面表達式註解
註解是Java中非常重要的一種語法標記,Spring AOP中也通過註解的方式來定義切面。在定義切面時,可以使用@Aspect註解表示一個切面類,也可以使用@Before、@After、@Around等註解表示具體的切面執行時機。下面是一個使用註解定義切面並且在方法執行之前和之後列印日誌的示例:
@Aspect public class LogAspect { @Before("execution(* com.example.demo.service.*.*(..))") public void beforeLog() { System.out.println("Before invoking method."); } @After("execution(* com.example.demo.service.*.*(..))") public void afterLog() { System.out.println("After invoking method."); } }
在該示例中,通過@Before和@After註解來表示在目標方法執行之前和之後執行。而@Aspect註解則表示該類是一個切面類。
二、Spring切面表達式
Spring AOP支持切點表達式,可以使用表達式來匹配滿足某些條件的方法進行切面的處理。Spring AOP中使用AspectJ的切點表達式,其語法與AspectJ基本一致,但也有一些不同之處。下面是一些常用的切點表達式:
- execution(* com.example.demo.service.*.*(..)):匹配com.example.demo.service包中所有類的所有方法。
- execution(public * com.example.demo.service.UserService.*(..)):匹配com.example.demo.service.UserService介面中所有public方法。
- execution(* com.example.demo.service.UserService+.*(..)):匹配com.example.demo.service.UserService介面及其所有實現類的所有方法。
- @annotation(com.example.demo.annotation.Log):匹配所有被@Log註解標記的方法。
- within(com.example.demo.service.*):匹配com.example.demo.service包中所有類的所有方法,但不包括子包中的。
三、切面表達式參數使用
除了上述示例中的切點表達式,還可以在切點表達式中使用參數。參數有兩種類型:JoinPoint和ProceedingJoinPoint。JoinPoint表示方法的執行時機,而ProceedingJoinPoint則表示帶有返回值的方法執行時機。下面是一個示例:
@Aspect public class LogAspect { @AfterReturning(value = "@annotation(com.example.demo.annotation.Log)", returning = "result") public void afterLog(JoinPoint joinPoint, Object result) { System.out.println("After invoking method: " + joinPoint.getSignature().getName() + ", result: " + result); } @Around("@annotation(com.example.demo.annotation.Log)") public Object aroundLog(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("Before invoking method: " + proceedingJoinPoint.getSignature().getName()); Object result = proceedingJoinPoint.proceed(); System.out.println("After invoking method: " + proceedingJoinPoint.getSignature().getName() + ", result: " + result); return result; } }
在該示例中,@AfterReturning註解表示在方法執行後列印方法名和返回值。而@Around註解則表示在方法執行前後列印日誌,並且通過proceed()方法繼續執行原始方法,並且可以獲取返回值。
四、切面表達式生效
在Spring AOP中,只有通過@Pointcut註解聲明的方法才能被其他切面方法調用。下面是一個示例:
@Aspect public class LogAspect { @Pointcut("@annotation(com.example.demo.annotation.Log)") public void logPointcut() {} @AfterReturning(value = "logPointcut()", returning = "result") public void afterLog(JoinPoint joinPoint, Object result) { System.out.println("After invoking method: " + joinPoint.getSignature().getName() + ", result: " + result); } }
在該示例中,通過@Pointcut註解聲明一個logPointcut()方法,在其他切面方法中可以使用logPointcut()方法。
五、二元函數切面的表達式
二元函數切面表示同時匹配兩個或多個切點的情況。在Spring AOP中,可以使用&&、||和!符號來實現二元函數切點。下面是一個示例:
@Aspect public class LogAspect { @AfterReturning(value = "logPointcut() && execution(* com.example.demo.service.UserService.*(..))", returning = "result") public void afterLog(JoinPoint joinPoint, Object result) { System.out.println("After invoking UserService method: " + joinPoint.getSignature().getName() + ", result: " + result); } }
在該示例中,使用&&符號同時匹配logPointcut()和UserService的所有方法。
六、AOP切面表達式書面表達格式選取
在書寫切面表達式時,應注意語法的正確性和表達式的簡潔性。下面是一些書寫表達式時需要注意的規則:
- 盡量使用通配符,減少代碼量。
- 使用括弧對表達式進行分組,使運算符優先順序更清晰。
- 使用點號分割包名和類名,便於閱讀。
- 使用註解、參數等限制條件來過濾切點,提高代碼的可讀性。
原創文章,作者:TNLCI,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/369469.html