栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot基础-AOP的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot基础-AOP的使用

AOP

AOP全称Aspect Oriented Programming意为面向切面编程,也叫做面向方法编程,是通过预编译方式和运行期动态代理的方式实现不修改源代码的情况下给程序动态统一添加功能的技术。

创建项目要 

导入AOP依赖

        
        
            aopalliance
            aopalliance
            1.0
        
        
        
            org.springframework
            spring-aspects
            5.0.2.RELEASE
        
        
        
            org.aspectj
            aspectjweaver
            1.8.3
        
注解的方式实现AOP

创建自定义注解

package com.example.noteback.test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//方法上添加该注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AopAnno {
    public String value();
}

创建Test类,test方法

package com.example.noteback.test;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Test {
    
    @RequestMapping("test")
    @AopAnno(value = "test")
    public String test(String str) {
        System.out.println("test方法" + str);
        return "test";
    }
}

创建AopTest类

package com.example.noteback.test;

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;

@Component
@Aspect
public class AopTest {

    //切入点
    @Pointcut("@annotation(com.example.noteback.test.AopAnno)")
    public void test() {
    }

}
@Before前置通知 

前置通知在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。使用方法如下:

@Component
@Aspect
public class AopTest {
    //切入点
    @Pointcut("@annotation(com.example.noteback.test.AopAnno)")
    public void test() {
    }

    //跟据方法名(上面的test方法)获取切入点
    @Before("test()")
    //获取要增强的方法参数,可以添加 JoinPoint
    public void before(JoinPoint joinPoint) {
        //获取方法签名
        MethodSignature methodSignature=(MethodSignature) joinPoint.getSignature();
        //获取要增强的方法
        Method method=methodSignature.getMethod();
        System.out.println("方法名"+method.getName());
    }
}
@Around环绕通知 

包围一个连接点的通知,类似 Web 中 Servlet 规范中的 Filter 的 doFilter 方法。可 以在方法的调用前后完成自定义的行为, 也可以选择不执行。使用方法如下:

@Component
@Aspect
public class AopTest {

    @Pointcut("@annotation(com.example.noteback.test.AopAnno)")
    public void test() {
    }

    @Around("test()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //要增强的方法执行前
        System.out.println("aroundBefore");
        //执行要增强的方法
        Object proceed = proceedingJoinPoint.proceed();
        //要增强的方法执行后
        System.out.println("aroundAfter");
        return proceed;
    }
}

通过@Around可以实现权限验证,操作日志等。实现操作日志:

@Component
@Aspect
public class AopTest {

    @Pointcut("@annotation(com.example.noteback.test.AopAnno)")
    public void test() {
    }

    @Around("test()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        MethodSignature methodSignature=(MethodSignature) proceedingJoinPoint.getSignature();
        Method method=methodSignature.getMethod();

        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("方法:"+method.toString());
        stringBuilder.append("t参数(");
        //参数名
        String[] parameterNames=methodSignature.getParameterNames();
        //参数值
        Object[] parameterValues=proceedingJoinPoint.getArgs();
        for (int i = 0; i < parameterNames.length; i++) {
            stringBuilder.append(parameterNames[i]+":"+parameterValues[i]+",");
        }
        stringBuilder.append(")");
        //允许目标方法执行
        Object proceed = proceedingJoinPoint.proceed();
        stringBuilder.append("t返回(");
        stringBuilder.append(method.getReturnType()+":"+proceed+")");
        System.out.println(stringBuilder);
        //返回执行结果
        return proceed;
    }
}
@After后置通知 

当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。

对Test类里面的值稍加修改

@RestController
public class Test {

    @RequestMapping("test")
    @AopAnno(value = "test")
    public String test(String str) {
        //正常情况下会报错,下面的方法不执行
        return (1/0)+"";
    }
}

@After使用方法如下:

package com.example.noteback.test;

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;
import java.util.Arrays;

@Component
@Aspect
public class AopTest {

    @Pointcut("@annotation(com.example.noteback.test.AopAnno)")
    public void test() {
    }

    @After("test()")
    public void after() {
        System.out.println("after");
    }
}

运行结果:

@AfterReturning

在某连接点正常完成后执行的通知,不包括抛出异常的情况。使用方法如下:

package com.example.noteback.test;

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;
import java.util.Arrays;

@Component
@Aspect
public class AopTest {

    @Pointcut("@annotation(com.example.noteback.test.AopAnno)")
    public void test() {
    }

    @AfterReturning("test()")
    public void afterReturning() {
        System.out.println("AfterReturning");
    }
}
 @AfterThrowing

在方法抛出异常退出时执行的通知。使用方法如下:

package com.example.noteback.test;

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;
import java.util.Arrays;

@Component
@Aspect
public class AopTest {

    @Pointcut("@annotation(com.example.noteback.test.AopAnno)")
    public void test() {
    }


    @AfterThrowing("test()")
    public void afterThrowing(){
        System.out.println("afterThrowing");
    }
}
执行顺序 

 AfterThrowing在发生异常时执行

源码 

gitee仓库:git@gitee.com:AGi_R/note.git

在命令行输入:git clone git@gitee.com:AGi_R/note.git 进行下载

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/924769.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号