springboot Aop 方法攔截、注解攔截

Action.java

//此注解只能修飾方法
@Target(ElementType.METHOD)
//當(dāng)前注解如何去保持
@Retention(RetentionPolicy.RUNTIME)
//生成到API文檔
@Documented
public @interface Action {
    String name();
}

AopConfig.java

//JAVA配置類
@Configuration
//Bean掃描器
@ComponentScan("com.wangzhi.springboot.aop.test")
//開啟spring對aspectJ的支持
@EnableAspectJAutoProxy
public class AopConfig {}

這個類下的方法我們采用注解來攔截

@Service
public class DemoAnnotationService {

    @Action( name = "注解式攔截的add操作")
    public void add() {}
}

這個類下的方法我們采用方法規(guī)則來攔截

@Service
public class DemoMethodService {
    public void add() {}
}

LogAspect.java

@Aspect
@Component
public class LogAspect {
    
    //定義切面
    @Pointcut("@annotation(com.wangzhi.springboot.aop.test.Action)")
    public void annotationPointCut() {}
    
    //聲明一個advice,并使用@pointCut定義的切點
    @After("annotationPointCut()")
    public void after(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //從切面中獲取當(dāng)前方法
        Method method = signature.getMethod();
        //得到了方,提取出他的注解
        Action action = method.getAnnotation(Action.class);
        //輸出
        System.out.println("注解式攔截" + action.name());
    }
    //定義方法攔截的規(guī)則
    @Before("execution(* com.wangzhi.springboot.aop.test.DemoMethodService.*(..))")
    public void before(JoinPoint joinPoint) {
        MethodSignature signature =  (MethodSignature) joinPoint.getSignature();
        //攔截了方法
        Method method = signature.getMethod();
        //直接獲取方法名字
        System.out.println("方法規(guī)則式攔截" + method.getName());
    }
}

啟動器

public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
        DemoMethodService methodService = context.getBean(DemoMethodService.class);
        DemoAnnotationService annotationService = context.getBean(DemoAnnotationService.class);
        
        annotationService.add();
        methodService.add();
        
        context.close();
    }
}

pom.xml依賴

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.12.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.10</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.10</version>
    </dependency>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,283評論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,678評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,276評論 25 708
  • 持續(xù)分享85天,20171007。張紅 隨著時間的流逝,很快要到了上班時間了,我倒比較期待。假期里陰雨綿綿,整...
    啊呦a7_94閱讀 258評論 2 2
  • 一、互聯(lián)網(wǎng)碎片化時代,需要具備的三種能力 1、碎片整理能力 有效方法:思維導(dǎo)圖 2、多線程處理能力 3、放空能力(...
    d3ed2c762be7閱讀 324評論 0 0

友情鏈接更多精彩內(nèi)容