日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringAop @Pointcut(“@annotation“)\@Aspect练习

發布時間:2025/3/12 javascript 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringAop @Pointcut(“@annotation“)\@Aspect练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

切面記錄日志

切面類

@Slf4j @Aspect @Component public class AspectForFeign {@Pointcut("execution(public * com.keke.remote..*Feign.*(..))")public void pointcut() {}@Around("pointcut()")public Object around(ProceedingJoinPoint joinPoint) throws Throwable {long start = System.currentTimeMillis();log.info("@Around:開始執行目標方法:{}ms", start);Object result = null;try {result = joinPoint.proceed();} catch (Exception e) {System.out.println(e.getMessage());}long end = System.currentTimeMillis();log.info("@Around:結束執行目標方法:{}ms", end);//獲取方法簽名 MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();String methodName = methodSignature.getName();log.info("方法名:{} 耗時:{}ms", methodName, end - start);//如果不返回result,則目標對象實際返回值會被置為nullreturn result;}
  • 注意:返回結果如果不返回result,則目標對象實際返回值會被置為null

@Component

  • 需要將切面類配置為bean

@Aspect

  • 標記為切面類

@Pointcut

  • 需要表達式命名,而不需要在方法體內編寫實際代碼。
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)

@Around

  • 環繞增強,@Pointcut和@Around聯合使用等同于:
@Around("execution(public * com.keke.remote..*Feign.*(..))")

切點攔截記錄訪問日志

攔截器數據源

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)//注解在方法上 public @interface ApiAccess{ }

攔截器業務實現代碼

@Aspect @Component @Slf4j public class ApiAccessAspect {@Pointcut("@annotation(com.keke.annotation.ApiAccess)")public void pointcut() {}@Around("pointcut()")public Object apiAccessAspect(ProceedingJoinPoint joinPoint) throws Throwable {HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();log.info("url:{}被訪問了.....",request.getRequestURL().toString());return joinPoint.proceed(joinPoint.getArgs());} }

@Pointcut的表達式-@annotation

限制連接點的匹配,其中連接點的主題(在 Spring AOP 中執行的方法)具有給定的 annotation。

官方案例:

任何連接點(僅在 Spring AOP 中執行方法),其中執行方法具有@Transactional annotation:

@annotation(org.springframework.transaction.annotation.Transactional)

官方的案例已經說的很清楚了,就是@annotation是匹配擁有指定注解的方法的。這里要注意,@annotation只匹配實現類中的有注解的方法,不會匹配接口中的注解方法。

看案例:

我們準備接口:

/*** @description*/ public interface IBookService {//@DkAnnotation// 這里的注解是不會被匹配的public void saveBook(String title); }

實現類:

/*** @description*/ @Component public class BookService implements IBookService{@Override@DkAnnotation //這里的注解會被匹配public void saveBook(String title){System.out.println("保存圖書:"+title);}public void saveBook(String title,int count){System.out.println("保存"+title+","+count+"次");} }

修改Aspect類:

/*** @description*/ @Component //將當前bean交給spring管理 @Aspect //定義為一個AspectBean public class DkAspect {//使用@annotation配置匹配所有還有指定注解的方法@Pointcut("@annotation(com.st.dk.demo7.annotations.DkAnnotation)")private void pointCut1(){}//定義一個前置通知@Before("pointCut1()")private static void befor(){System.out.println("---前置通知---");} }

測試:

@Testpublic void testAopPoint_annotation(){ApplicationContext ac =new AnnotationConfigApplicationContext(Appconfig.class);IBookService bean = ac.getBean(IBookService.class);bean.saveBook("程序員的修養");}

結果:

總結

以上是生活随笔為你收集整理的SpringAop @Pointcut(“@annotation“)\@Aspect练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。