2. Spring4.x AOP

AOP的基本使用

  1. pom.xml中導(dǎo)入依賴
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
  1. 新建一個(gè)攔截規(guī)則的注解
package com.xiaohan.aop;

import java.lang.annotation.*;

/**
 * 攔截規(guī)則的注解
 */

@Target(ElementType.METHOD)             // 限制該注解只能使用在方法基本上
@Retention(RetentionPolicy.RUNTIME)     // 保留策略 運(yùn)行時(shí)可見
@Documented
public @interface SysLog {
    String name();
}
  1. 定義被攔截的類
package com.xiaohan.aop;

import org.springframework.stereotype.Service;

//使用 方法規(guī)則的 被攔截類
@Service
public class DemoMethodService {

    public void add() {
    }
}
package com.xiaohan.aop;

import org.springframework.stereotype.Service;

//使用 注解的 被攔截類
@Service
public class DemoAnnotionService {

    @SysLog(name = "注解方式攔截的add操作")
    public void add() {
    }
}
  1. 定義切面類
    @Before 前置通知 在目標(biāo)方法執(zhí)行之前執(zhí)行
    @After 后置通知 在目標(biāo)方法執(zhí)行之后執(zhí)行 無(wú)論是否發(fā)生異常
    @AfterReturning 返回通知 在目標(biāo)方法返回結(jié)果之后執(zhí)行
    @AfterThrowing 異常通知 在目標(biāo)方法拋出異常之后通知
    @Around 環(huán)繞通知 圍繞著目標(biāo)方法執(zhí)行
package com.xiaohan.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;

//  聲明這是一個(gè)切面類
@Aspect
@Component
public class LogAspect {

    //  定義攔截規(guī)則(切點(diǎn))
    @Pointcut("@annotation(com.xiaohan.aop.SysLog)")
    public void annotationPointCut() {
    }

    // 使用@Pointcut定義的切點(diǎn)規(guī)則
    @After("annotationPointCut()")
    public void after(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SysLog sysLog = method.getAnnotation(SysLog.class);
        System.err.println("后置通知 " + sysLog.name());
    }

    // 使用execution表達(dá)式定義的切點(diǎn)規(guī)則
    @Before("execution(* com.xiaohan.aop.DemoMethodService.*(..))")
    public void before(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.err.println("前置通知 " + "方法規(guī)則方式攔截的方法" + method.getName());
    }

    // 環(huán)繞通知是唯一一個(gè)可以 終止連接點(diǎn)(方法)執(zhí)行 的通知
    @Around("annotationPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        long beginTime = System.currentTimeMillis();
        //執(zhí)行方法
        Object result = point.proceed();
        //執(zhí)行時(shí)長(zhǎng)(毫秒)
        long time = System.currentTimeMillis() - beginTime;
        System.err.println("環(huán)繞通知 攔截" + "注解的" + method.getName() + "方法執(zhí)行了" + time + "毫秒");
        return result;
    }
}
  1. 新建一個(gè)配置類
package com.xiaohan.aop;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan("com.xiaohan.aop")

//開啟Spring對(duì)AspectJ代理的支持
@EnableAspectJAutoProxy
public class AopConfig {
}
  1. 創(chuàng)建一個(gè)Main方法 初始化容器 并測(cè)試
package com.xiaohan.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

// AOP
public class Main {
    public static void main(String[] args) {
        ApplicationContext ac = new AnnotationConfigApplicationContext(AopConfig.class);
        DemoMethodService demoMethodService = ac.getBean(DemoMethodService.class);
        demoMethodService.add();
        DemoAnnotionService demoAnnotionService = ac.getBean(DemoAnnotionService.class);
        demoAnnotionService.add();
    }
}

輸出

前置通知 方法規(guī)則方式攔截的方法add
環(huán)繞通知 攔截注解的add方法執(zhí)行了7毫秒
后置通知 注解方式攔截的add操作
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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