當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringAop @Pointcut(“@annotation“)\@Aspect练习
生活随笔
收集整理的這篇文章主要介紹了
SpringAop @Pointcut(“@annotation“)\@Aspect练习
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
切面記錄日志
切面類
@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:開始執(zhí)行目標方法:{}ms", start);Object result = null;try {result = joinPoint.proceed();} catch (Exception e) {System.out.println(e.getMessage());}long end = System.currentTimeMillis();log.info("@Around:結束執(zhí)行目標方法:{}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
- 需要表達式命名,而不需要在方法體內編寫實際代碼。
@Around
- 環(huán)繞增強,@Pointcut和@Around聯(lián)合使用等同于:
切點攔截記錄訪問日志
攔截器數(shù)據(jù)源
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)//注解在方法上 public @interface ApiAccess{ }攔截器業(yè)務實現(xiàn)代碼
@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 中執(zhí)行的方法)具有給定的 annotation。
官方案例:
任何連接點(僅在 Spring AOP 中執(zhí)行方法),其中執(zhí)行方法具有@Transactional annotation:
@annotation(org.springframework.transaction.annotation.Transactional)官方的案例已經說的很清楚了,就是@annotation是匹配擁有指定注解的方法的。這里要注意,@annotation只匹配實現(xiàn)類中的有注解的方法,不會匹配接口中的注解方法。
看案例:
我們準備接口:
/*** @description*/ public interface IBookService {//@DkAnnotation// 這里的注解是不會被匹配的public void saveBook(String title); }實現(xiàn)類:
/*** @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("程序員的修養(yǎng)");}結果:
總結
以上是生活随笔為你收集整理的SpringAop @Pointcut(“@annotation“)\@Aspect练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IoT -- (八)MQTT优缺点
- 下一篇: mxnet基础到提高(53)-批量标准化