基于注解的AOP

Posted on 2024-01-15 00:34  弯弓射雕的男人  阅读(5)  评论(0编辑  收藏  举报

1. 添加依赖

2. 准备被代理的目标资源

  2.1接口

  2.2实现类

3.切面类

  3.1 切入点

  3.2 通知类型

 

1添加依赖 

spring-aop 和 spring-aspects
 <!--spring aop依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>6.0.2</version>
        </dependency>

        <!--spring aspects依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>6.0.2</version>
        </dependency>

2. 准备被代理的目标资源

  2.1接口

package com.atguigu.Aop.AnnoAop;

public interface    Calculator {

    int add(int i, int j);

    int sub(int i, int j);

    int mul(int i, int j);

    int div(int i, int j);
}

 

  2.2实现类

package com.atguigu.Aop.AnnoAop;

public class CalculatorImpl implements Calculator {

    @Override
    public int add(int i, int j) {

        int result = i + j;

        System.out.println("方法内部 result = " + result);

        return result;
    }

    @Override
    public int sub(int i, int j) {

        int result = i - j;

        System.out.println("方法内部 result = " + result);

        return result;
    }

    @Override
    public int mul(int i, int j) {

        int result = i * j;

        System.out.println("方法内部 result = " + result);

        return result;
    }

    @Override
    public int div(int i, int j) {

        int result = i / j;

        System.out.println("方法内部 result = " + result);

        return result;
    }
}

3.切面类

  3.1 切入点

    切入点表达式

例如 

@Before("execution(public int com.atguigu.Aop.AnnoAop.CalculatorImpl.*(..))")


 

  3.2 通知类型

@Before()前置
@AfterReturning返回
@AfterThrowing 异常
@After() 环绕
@Around() 后置

具体的切面类
package com.atguigu.Aop.AnnoAop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.lang.reflect.Array;
import java.util.Arrays;

//切面类
@Component//ioc容器管理
@Aspect //表示这个类是切面类
public class LogAspect {
//设置切入点和通知类型

//通知类型..前置 返回 异常 环绕 后置

/*
@Before()前置
@AfterReturning返回
@AfterThrowing 异常
@After() 后置
@Around() 环绕
*/

//前置通知
@Before("execution(public int com.atguigu.Aop.AnnoAop.CalculatorImpl.*(..))")
public void before(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
Object[] args=joinPoint.getArgs();
System.out.println("Before = " + "前置通知"+"方法名:"+methodName+"参数"+ Arrays.toString(args));
}
//后置 通知
@After("execution(public int com.atguigu.Aop.AnnoAop.CalculatorImpl.*(..))")
public void After(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
System.out.println("After = " + "后置通知"+"方法名:" +methodName);
}
//返回通知
@AfterReturning(value = "execution(public int com.atguigu.Aop.AnnoAop.CalculatorImpl.*(..))",returning = "result")
public void AfterReturning(JoinPoint joinPoint,Object result){
String methodName=joinPoint.getSignature().getName();
Object[] args=joinPoint.getArgs();
System.out.println("AfterReturning = " + "返回通知"+"方法名:"+methodName+"参数"+ Arrays.toString(args));
}
//出现异常方法执行
@AfterThrowing(value = "execution(public int com.atguigu.Aop.AnnoAop.CalculatorImpl.*(..))",throwing="ex")
public void AfterThrowing(JoinPoint joinPoint,Throwable ex){

String methodName=joinPoint.getSignature().getName();
Object[] args=joinPoint.getArgs();
System.out.println("AfterThrowing = " + "异常通知"+"方法名:"+methodName+"异常信息"+ex);
}
//环绕通知
@Around("execution(public int com.atguigu.Aop.AnnoAop.CalculatorImpl.*(..))")
public void Around(ProceedingJoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
Object[] args=joinPoint.getArgs();
String argString= Arrays.toString(args);
Object result=null;

try {
System.out.println("环绕通知==目标方法之前");
result = joinPoint.proceed();
System.out.println("环绕通知==目标方法之后");
}catch (Throwable throwable){
throwable.printStackTrace();
System.out.println("出现异常 的时候执行 ");
}finally {
System.out.println("执行完毕 的时候执行");
}

System.out.println("Around = " + "异常通知"+"方法名:"+methodName);
}



}