Spring的概念以及基于注解实现AOP

AOP的概念:

AOP(Aspect Oriented Programming)是一种设计思想,是软件设计领域中的面向切面编程,它是面向对象编程的一种补充和完善,它以通过预编译方式和运行期动态代理方式实在不修改源代码的情况下给程序动态统一添加额外功能的一种技术。

横切关注点:从每个方法中抽取出来的同一类非核心业务

通知:横切关注点的代理实现方法

切面:包装通知的类

目标:被代理的目标对象

代理:向目标对象应用通知之后创建的代理对象

连接点:是概念性的,把方法排成一排,每一个横切位置看成x轴方向,把方法从上到下执行的顺序看成y轴,x轴和y轴的交叉点就是连接点。

切入点:定位连接点的方式

AOP的作用:

简化代码:把方法中固定位置的重复的代码抽取出来,让被抽取的方法更专注于自己的核心功能,
提高内聚性。
代码增强:把特定的功能封装到切面类中,看哪里有需要,就往上套,被套用了切面逻辑的方法就
被切面给增强了。

一 基于注解的AOP之准备工作

在IOC所需依赖基础上再加入下面依赖即可: 

<!-- spring-aspects会帮我们传递过来aspectjweaver -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>5.3.1</version>
</dependency>

 目标资源:

接口:

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);
 
}

实现类:

@Component
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;
    }
}

在Spring的配置文件中配置:

<!--
    基于注解的AOP的实现:
    1、将目标对象和切面交给IOC容器管理(注解+扫描)
    2、开启AspectJ的自动代理,为目标对象自动生成代理
    3、将切面类通过注解@Aspect标识
  -->
  <context:component-scan base-package="com.atguigu.aop.annotation">
</context:component-scan>
  <aop:aspectj-autoproxy />

AOP的注意事项:
切面类和目标类都需要交给IOC容器管理
切面类必须通过@Aspect注解标识为一个切面
在Spring的配置文件中设置<aop:aspectj-autoproxy />开启基于注解的AOP

                                                                                                                                                         

 创建切面类并配置:

在Spring配置文件中配置:

<!--开启基于注解的AOP-->
    <aop:aspectj-autoproxy />

各种通知:

前置通知:使用@Before注解标识,在被代理的目标方法前执行
返回通知:使用@AfterReturning注解标识,在被代理的目标方法成功结束后执行(寿终正寝)
异常通知:使用@AfterThrowing注解标识,在被代理的目标方法异常结束后执行(死于非命)
后置通知:使用@After注解标识,在被代理的目标方法最终结束后执行(盖棺定论)
环绕通知:使用@Around注解标识,使用try...catch...finally结构围绕整个被代理的目标方法,包
括上面四种通知对应的所有位置

前置通知:

@Before("execution(public int com.atguigu.spring.aop.annotation.CalculatorImpl.add(int, int))")
public void beforeAdviceMethod(JoinPoint joinPoint) {
        //获取连接点所对应方法的签名信息
        Signature signature = joinPoint.getSignature();
        //获取连接点所对应方法的参数
        Object[] args = joinPoint.getArgs();
        System.out.println("LoggerAspect,方法:"+signature.getName()+",参数:"+ Arrays.toString(args));
    }

当前包下所有类都前置通知: 

@Pointcut("execution(* com.atguigu.aop.annotation.*.*(..))")
public void beforeAdviceMethod(JoinPoint joinPoint) {
        //获取连接点所对应方法的签名信息
        Signature signature = joinPoint.getSignature();
        //获取连接点所对应方法的参数
        Object[] args = joinPoint.getArgs();
        System.out.println("LoggerAspect,方法:"+signature.getName()+",参数:"+ Arrays.toString(args));
    }

 声明公共切入点表达式:

@Pointcut("execution(* com.atguigu.aop.annotation.*.*(..))")
public void pointCut(){}

使用公共契入点表达式:

@Before(“pointCut()”) 

第一个*表示任意的访问修饰符和返回值类型

第二个*表示类中的任意的方法

..表示任意的参数列表 

包的地方也可以使用*,表示所有包

在同一个切面中使用:

@Before("pointCut()")
public void beforeMethod(JoinPoint joinPoint){
  String methodName = joinPoint.getSignature().getName();
  String args = Arrays.toString(joinPoint.getArgs());
  System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
}

 在不同切面中使用:

@Before("com.atguigu.aop.CommonPointCut.pointCut()")
public void beforeMethod(JoinPoint joinPoint){
  String methodName = joinPoint.getSignature().getName();
  String args = Arrays.toString(joinPoint.getArgs());
  System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
}

获取连接点信息:

@Before("execution(public int com.atguigu.aop.annotation.CalculatorImpl.*(..))")
public void beforeMethod(JoinPoint joinPoint){
  //获取连接点的签名信息
  String methodName = joinPoint.getSignature().getName();
  //获取目标方法到的实参信息
  String args = Arrays.toString(joinPoint.getArgs());
  System.out.println("Logger-->前置通知,方法名:"+methodName+",参数:"+args);
}

获取目标方法的返回值:

@AfterReturning中的属性returning,用来将通知方法的某个形参,接收目标方法的返回值

@AfterReturning(value = "execution(* com.atguigu.aop.annotation.CalculatorImpl.*
(..))", returning = "result")
public void afterReturningMethod(JoinPoint joinPoint, Object result){
  String methodName = joinPoint.getSignature().getName();
  System.out.println("Logger-->返回通知,方法名:"+methodName+",结果:"+result);
}

 获取目标方法的异常:

@AfterThrowing(value = "execution(* com.atguigu.aop.annotation.CalculatorImpl.*
(..))", throwing = "ex")
public void afterThrowingMethod(JoinPoint joinPoint, Throwable ex){
  String methodName = joinPoint.getSignature().getName();
  System.out.println("Logger-->异常通知,方法名:"+methodName+",异常:"+ex);
}

环绕通知:

@Around("execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))")
public Object aroundMethod(ProceedingJoinPoint joinPoint){
  String methodName = joinPoint.getSignature().getName();
  String args = Arrays.toString(joinPoint.getArgs());
  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("环绕通知-->目标对象方法执行完毕");
 }
  return result;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值