Spring AOP基于@AspectJ注解的切面

2024-03-13 17:20

本文主要是介绍Spring AOP基于@AspectJ注解的切面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

该篇博客讲述基于AspectJ的注解方式实现AOP切面,关于AOP的一些概念性问题可以转战https://blog.csdn.net/w_linux/article/details/80194768

该篇博客主要阐述

1、编写切点(@Pointcut)
2、基于AspectJ的注解方式实现日志打印
3、环绕通知用法
4、JoinPoint用法

一、编写切点(@Pointcut)

@Pointcut需要在切面中使用,如下

这里写图片描述

Pointcut定义时,还可以使用&&、||、! 这三个运算

这里写图片描述

编写切点表达式

这里写图片描述

AspectJ指示器

当我们查看这些Spring支持的指示器时,注意只有execution指示器是唯一的执行匹配,而其他的指示器都是用于限制匹配的。这说明execution指示器是我们在编写切点定义时最主要使用的指示器

这里写图片描述


二、基于AspectJ的注解方式实现日志打印

1、场景分析

在一个经典的业务逻辑(加减乘除)中需要每次执行某个方法时,打印一些增强该业务逻辑的功能,比如某个方法执行完毕后提示用户执行完毕等日志功能

2、解决方案

使用基于@AspectJ的注解方式来实现日志打印,既不用在核心业务方法中添加一些非核心代码,又能实现自己想要实现的功能,利用前置通知、后置通知、返回通知、异常通知实现

前提:使用AspectJ注解需要依赖于以下几个类库
  • aspectjweaver-1.6.10.jar
  • spring-aop-4.3.10.RELEASE.jar
  • spring-aspects-4.3.10.RELEASE.jar

当然最省力的办法就是将所有Spring的jar都加到classpath中

3、代码

Arithmetic.java(核心业务代码)

package com.linjie.aop;import org.springframework.stereotype.Component;
/**
* @author LinJie E-mail:ash-ali@163.com
* @version 创建时间:2018年5月6日 下午7:53:56
* @核心业务:基本运算
*/
@Component("arithmetic")
public class Arithmetic {//addpublic int add(int d,int e) {System.out.println("add method END!");System.out.println();return d+e;}//subtractionpublic int sub(int a,int b) {System.out.println("sub method END!");System.out.println();return a-b;}//multiplicativepublic int mul(int a,int b) {System.out.println("mul method END!");System.out.println();return a*b;}//divisionpublic int div(int a,int b) {System.out.println("div method END!");System.out.println();return a/b;}
}

LogginAspectJ.java(基于AspectJ的注解方式的切面——日志)

package com.linjie.aop;import java.util.Arrays;import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;/*** @author LinJie* log功能,不影响核心业务*/
@Component("logginAspectJ")
@Aspect
public class LogginAspectJ {/**定义一个方法,用于声明切点表达式,该方法一般没有方法体*@Pointcut用来声明切点表达式*通知直接使用定义的方法名即可引入当前的切点表达式 */@Pointcut("execution(* com.linjie.aop.Arithmetic.*(..))")public void PointcutDeclaration() {}//前置通知,方法执行之前执行@Before("PointcutDeclaration()")public void BeforeMethod(JoinPoint jp) {    String methodName = jp.getSignature().getName();Object[] args = jp.getArgs();System.out.println("BeforeMethod  The method   "+ methodName +"   parameter is  "+ Arrays.asList(args));System.out.println("add before");System.out.println();}//后置通知,方法执行之后执行(不管是否发生异常)@After("PointcutDeclaration()")public void AfterMethod(JoinPoint jp) {String methodName = jp.getSignature().getName();Object[] args = jp.getArgs();System.out.println("AfterMethod  The method    "+ methodName +"   parameter is  "+Arrays.asList(args));System.out.println();}//返回通知,方法正常执行完毕之后执行@AfterReturning(value="PointcutDeclaration()",returning="result")public void AfterReturningMethod(JoinPoint jp,Object result) {String methodName = jp.getSignature().getName();Object[] args = jp.getArgs();System.out.println("AfterReturningMethod  The method   "+ methodName +"   parameter is  "+Arrays.asList(args)+" "+result);System.out.println();} //异常通知,在方法抛出异常之后执行@AfterThrowing(value="PointcutDeclaration()",throwing="e")public void AfterThrowingMethod(JoinPoint jp,Exception e) {String methodName = jp.getSignature().getName();System.out.println("AfterThrowingMethod  The method   "+ methodName +"exception :"+e);}
}

applicationContext.xml(用于配置Bean和aop切面)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 扫描 --><context:component-scan base-package="com.linjie.aop"></context:component-scan>  <!-- 使AspectJ注解自动为匹配的类生成代理对象 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>

在Spring IoC容器中启动AspectJ注解支持,只要在Bean的配置文件中定义一个空的XML元素

 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

测试类

package com.linjie.aop;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @author LinJie* 测试*/
public class SpringTest {@Testpublic void test() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Arithmetic arithmetic = (Arithmetic) context.getBean("arithmetic");int result = arithmetic.add(1, 2);System.out.println("result: "+result);System.out.println("--------------");result = arithmetic.div(8, 1);System.out.println("result: "+result);}
}

运行结果

这里写图片描述


三、环绕通知用法

环绕通知是最为强大的通知,这里与以上的切面不同以外其他都相同

这里写图片描述


四、JoinPoint用法

可以在上面的切面代码中发现参数JoinPoint,下面就阐述下该参数的用法

JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象.

常用方法

这里写图片描述

ProceedingJoinPoint对象

ProceedingJoinPoint对象是JoinPoint的子接口,该对象用在@Around的切面方法中,常用有以下两个方法

这里写图片描述


参考

《Spring IN ACTION》

这篇关于Spring AOP基于@AspectJ注解的切面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/805601

相关文章

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java进程异常故障定位及排查过程

《Java进程异常故障定位及排查过程》:本文主要介绍Java进程异常故障定位及排查过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、故障发现与初步判断1. 监控系统告警2. 日志初步分析二、核心排查工具与步骤1. 进程状态检查2. CPU 飙升问题3. 内存

java中新生代和老生代的关系说明

《java中新生代和老生代的关系说明》:本文主要介绍java中新生代和老生代的关系说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、内存区域划分新生代老年代二、对象生命周期与晋升流程三、新生代与老年代的协作机制1. 跨代引用处理2. 动态年龄判定3. 空间分

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一