當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
SpringAop @Pointcut(“@annotation“)\@Aspect练习
生活随笔
收集整理的這篇文章主要介紹了
SpringAop @Pointcut(“@annotation“)\@Aspect练习
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
切面記錄日志
切面類
@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í)行目標(biāo)方法:{}ms", start);Object result = null;try {result = joinPoint.proceed();} catch (Exception e) {System.out.println(e.getMessage());}long end = System.currentTimeMillis();log.info("@Around:結(jié)束執(zhí)行目標(biāo)方法:{}ms", end);//獲取方法簽名 MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();String methodName = methodSignature.getName();log.info("方法名:{} 耗時(shí):{}ms", methodName, end - start);//如果不返回result,則目標(biāo)對(duì)象實(shí)際返回值會(huì)被置為nullreturn result;}- 注意:返回結(jié)果如果不返回result,則目標(biāo)對(duì)象實(shí)際返回值會(huì)被置為null
@Component
- 需要將切面類配置為bean
@Aspect
- 標(biāo)記為切面類
@Pointcut
- 需要表達(dá)式命名,而不需要在方法體內(nèi)編寫實(shí)際代碼。
@Around
- 環(huán)繞增強(qiáng),@Pointcut和@Around聯(lián)合使用等同于:
切點(diǎn)攔截記錄訪問(wèn)日志
攔截器數(shù)據(jù)源
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)//注解在方法上 public @interface ApiAccess{ }攔截器業(yè)務(wù)實(shí)現(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:{}被訪問(wèn)了.....",request.getRequestURL().toString());return joinPoint.proceed(joinPoint.getArgs());} }@Pointcut的表達(dá)式-@annotation
限制連接點(diǎn)的匹配,其中連接點(diǎn)的主題(在 Spring AOP 中執(zhí)行的方法)具有給定的 annotation。
官方案例:
任何連接點(diǎn)(僅在 Spring AOP 中執(zhí)行方法),其中執(zhí)行方法具有@Transactional annotation:
@annotation(org.springframework.transaction.annotation.Transactional)官方的案例已經(jīng)說(shuō)的很清楚了,就是@annotation是匹配擁有指定注解的方法的。這里要注意,@annotation只匹配實(shí)現(xiàn)類中的有注解的方法,不會(huì)匹配接口中的注解方法。
看案例:
我們準(zhǔn)備接口:
/*** @description*/ public interface IBookService {//@DkAnnotation// 這里的注解是不會(huì)被匹配的public void saveBook(String title); }實(shí)現(xiàn)類:
/*** @description*/ @Component public class BookService implements IBookService{@Override@DkAnnotation //這里的注解會(huì)被匹配public void saveBook(String title){System.out.println("保存圖書:"+title);}public void saveBook(String title,int count){System.out.println("保存"+title+","+count+"次");} }修改Aspect類:
/*** @description*/ @Component //將當(dāng)前bean交給spring管理 @Aspect //定義為一個(gè)AspectBean public class DkAspect {//使用@annotation配置匹配所有還有指定注解的方法@Pointcut("@annotation(com.st.dk.demo7.annotations.DkAnnotation)")private void pointCut1(){}//定義一個(gè)前置通知@Before("pointCut1()")private static void befor(){System.out.println("---前置通知---");} }測(cè)試:
@Testpublic void testAopPoint_annotation(){ApplicationContext ac =new AnnotationConfigApplicationContext(Appconfig.class);IBookService bean = ac.getBean(IBookService.class);bean.saveBook("程序員的修養(yǎng)");}結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的SpringAop @Pointcut(“@annotation“)\@Aspect练习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: IoT -- (八)MQTT优缺点
- 下一篇: mxnet基础到提高(53)-批量标准化