最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Spring AOP中AspectJ切入點(diǎn)表達(dá)式的巧妙利用

標(biāo)簽:
Java Spring

在项目中需要利用Spring AOP对所有的controller方法进行拦截,很容易想到利用切入点表达式中的@annotation对RequestMapping注解进行切面编程。例如:

    @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public Object actionAround(ProceedingJoinPoint pjp) throws Throwable {

        Object result = pjp.proceed();
        System.out.println("actionAround");
        return result;
    }

然而这种情况下,如果controller上是GetMapping,则不会生效,例如下面这种

@RestController
public class FooController {

    @GetMapping("foo")
    public String foo() {
        return "foo";
    }
}

因此要把GetMapping,PutMapping,PostMapping这些都写上去,切面表达式修改成了

    @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)"
            + "|| @annotation(org.springframework.web.bind.annotation.GetMapping)"
            + "|| @annotation(org.springframework.web.bind.annotation.PostMapping)"
            + "|| @annotation(org.springframework.web.bind.annotation.DeleteMapping)"
            + "|| @annotation(org.springframework.web.bind.annotation.PutMapping)"
            + "|| @annotation(org.springframework.web.bind.annotation.PatchMapping)"
    )
    public Object actionAround(ProceedingJoinPoint pjp) throws Throwable {

        Object result = pjp.proceed();
        System.out.println("actionAround");
        return result;
    }

这样到时达到要求了,但是感觉代码行数太多,有没有什么巧妙的方法呢?

一开始想到了利用通配符,匹配org.springframework.web.bind.annotation包里所有以Mapping结尾的注解,改成

    @Around("@annotation(org.springframework.web.bind.annotation.*Mapping)")
    public Object actionAround(ProceedingJoinPoint pjp) throws Throwable {

        Object result = pjp.proceed();
        System.out.println("actionAround");
        return result;
    }

然而@annotation并不支持通配符,看一下@annotation的定义:

AtAnnotation := ‘@annotation’ ‘(’ AnnotationOrIdentifier ‘)’

就是说@annotation后面括号里是AnnotationOrIdentifier,那么AnnotationOrIdentifier定义又是什么呢?

AnnotationOrIdentifier := FullyQualifiedName | Identifier

看样子不像能写通配符的样子。

仔细看了一下,GetMapping,PostMapping这些注解实际上就是带有RequestMapping的注解
例如GetMapping就带有了RequestMapping,并且设置了method

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.GET)
public @interface GetMapping

所以切面表达式应该是满足一下两种条件:

  1. 带有RequestMapping注解的方法
  2. 带有带有RequestMapping注解的注解的方法
    @Around("execution(@(@org.springframework.web.bind.annotation.RequestMapping *) * *(..))" +
            "|| @annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public Object actionAround(ProceedingJoinPoint pjp) throws Throwable {

        Object result = pjp.proceed();
        System.out.println("actionAround");
        return result;
    }

另外由于带有RequestMapping的注解也都在·org.springframework.web.bind.annotation·包中,并且以Mapping结尾,因此可以使用如下表达式一网打尽:

    @Around("execution(@(org.springframework.web.bind.annotation.*Mapping) * *(..))")
    public Object actionAround(ProceedingJoinPoint pjp) throws Throwable {

        Object result = pjp.proceed();
        System.out.println("actionAround");
        return result;
    }
點(diǎn)擊查看更多內(nèi)容
2人點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消