Spring AOP的概念与实现过程详解

作者:绿仔牛奶_ 时间:2023-02-17 02:54:12 

Aop

什么是Aop?

AOP就是面向切面编程,通过预编译方式以及运行期间的 * 技术来实现程序的统一维护功能。

什么是切面,我理解的切面就是两个方法之间,两个对象之间,两个模块之间就是一个切面。假设在两个模块之间需要共同执行一系列操作,并且最后将这一系列操作注入到两个模块之间的指定位置。此时这一系列操作就是切面,注入这些操作的位置称之为切点。

举例:公司员工上班

A员工上班需要在前台进行打卡,同样的B员工…其他员工都需要在前台打卡,那么如果为每一位员工提供单独的打卡通道就有些过于浪费资源。像这样

Spring AOP的概念与实现过程详解

于是,在前台这个位置设置接口,声明公共的打卡方法,所有员工共同通过该接口进行打卡,那么在打卡时的一系列校验或者记录的整个操作就可以被称之为切面,前台这个空间位置就被称之为切点,如下图所示

Spring AOP的概念与实现过程详解

aop主要作用就是进行日志记录、事务/异常处理等功能以便更好地维护开发,主要目的就是将这些行为从项目的业务逻辑代码中分离出来并且降低各个业务逻辑模块之间的耦合度。保证在执行aop操作的同时不会影响到项目的业务逻辑代码

几个概念

Aspect:切面 Aspect中会包含一些pointCut切入点以及一些Advice

Advice:通知 切面需要完成的工作,通过before、after、around来区别是在连接点之前或者之后 或者环绕

Target:目标 目标对象,该对象会被织入advice

PointCut:切点 即切面通知执行的地点 advice将会在这里发生

JointPoint:连接点 所有方法的执行点

PointCut用来修饰JointPoint,PointCut是advice执行的点,而JointPoint表示所有方法的执行点,通过PointCut可以确定哪些JointPoint是可以被织入的点

我们通常不希望advice会在所有的JointPoint点执行,PointCut的作用就是可以进行校验来更精准的匹配执行点。简单概括就是Jointpoint可以执行但未必执行,只有PointCut匹配到了JointPoint才可以在该点执行

Aspect切面可以理解为PointCut+Advice

使用AOP织入导入包

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjweaver</artifactId>
 <version>1.9.7</version>
 <scope>runtime</scope>
</dependency>

实现aop方式一

使用spring内置的API接口

准备:UserService、UserServiceImpl简单实现CRUD

在spring核心配置文件applicationContext.xml中配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans      https://www.springframework.org/schema/beans/spring-beans.xsd                     http://www.springframework.org/schema/aop                  https://www.springframework.org/schema/aop/spring-aop.xsd">
 <bean id="userServiceImpl" class="com.mount.service.UserServiceImpl"/>
 <bean id="log" class="com.mount.log.log"/>
 <bean id="afterLog" class="com.mount.log.AfterLog"/>
 <!--  配置AOP  -->
 <aop:config>
   <!-- 切入点 expression表达式  表示从哪里开始执行   -->
   <aop:pointcut id="pointcut" expression="execution(* com.mount.service.UserServiceImpl.*(..))"/>
   <!--      执行环绕增强  -->
   <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
   <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
 </aop:config>
</beans>

编写执行前后日志

// 执行前  实现spring内置接口MethodBeforeAdvice
public class log implements MethodBeforeAdvice {
   public void before(Method method, Object[] args, Object target) throws Throwable {
       System.out.println(target.getClass().getName()+"执行了"+method.getName()+"方法");
   }
}
// 执行后 实现AfterReturningAdvice
public class AfterLog implements AfterReturningAdvice {
 public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
   System.out.println(target.getClass().getName()+"调用了"+method.getName()+"方法"+"返回的结果为"+returnValue);
 }
}

测试:

@Test
public void test01(){
 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 UserService bean = context.getBean("userServiceImpl", UserService.class);
 bean.insert();
}

实现aop方式二

使用自定义类,只需要在applicationContext.xml中重新配置aop,并且自己diy一个类即可,在配置时可以选择任一方法为前置日志或后置日志即可

<!--  配置AOP 方式二  -->
<bean id="diy" class="com.mount.diyLog.diyLogImpl"/>
<aop:config>
 <!--     自定义切面 ref引入的类   -->
 <aop:aspect ref="diy">
   <aop:pointcut id="pointcut" expression="execution(* com.mount.service.UserServiceImpl.*(..))"/>
   <aop:before method="before" pointcut-ref="pointcut"/>
   <aop:after method="after" pointcut-ref="pointcut"/>
 </aop:aspect>
</aop:config>

diylog类

public class diyLog {
 public void before(){
   System.out.println("===before方法执行===");
 }
 public void after(){
   System.out.println("===after方法执行===");
 }
}

注解实现aop

首先需要在applicationContext.xml文件中打开SpringAOP对注解的支持

<!-- SpringAop开启注解支持 -->
<aop:aspectj-autoproxy/>
<!-- 映射自定义注解实现log类 -->
<bean id="annoLog" class="com.mount.annoLog.annoLogImpl"/>

annoLog

// Aspect标注该类是一个切面
@Aspect
public class annoLogImpl {
 // 前置增强
 @Before("execution(* com.mount.service.UserServiceImpl.*(..))")
 public void before(){
   System.out.println("---方法执行前---");
 }
 // 后置增强
 @After("execution(* com.mount.service.UserServiceImpl.*(..))")
 public void after(){
   System.out.println("---方法执行后---");
 }
 // 环绕增强
 @Around("execution(* com.yuqu.dao.UserMapperImpl.*(..))")
   public Object around(ProceedingJoinPoint pjp) throws Throwable {
       System.out.println("环绕前");
       Object proceed = pjp.proceed();
       System.out.println("环绕后");
       System.out.println("执行信息:"+pjp.getSignature());
       return proceed;
   }
}

最终打印:

环绕前
---方法执行前---
删除成功!
---方法执行后---
方法签名Integer com.mount.service.UserService.delete()
方法执行返回=1
环绕后

注意around环绕增强,如果我们执行的sql中是有返回值的话,那么必须显式的将pjp.proceed();返回回去,否则在调用处将会无法获取到结果集,报空指针异常

可以发现,around环绕增强首先执行,在执行到joinPoint.proceed()时,会执行对应方法,执行对应方法的时候才会执行前置或后置的其他增强操作

来源:https://blog.csdn.net/yuqu1028/article/details/129017916

标签:Spring,AOP,概念,实现方式
0
投稿

猜你喜欢

  • Android 使用 SharedPreferences 保存少量数据的实现代码

    2023-07-03 01:00:11
  • C#中Linq查询基本操作使用实例

    2023-11-25 00:25:37
  • 如何搭建新的WPF项目框架

    2023-09-28 08:18:05
  • SpringCloud @FeignClient参数的用法解析

    2022-11-25 06:31:37
  • Java 线程同步详解

    2021-08-20 05:03:10
  • 引入mybatis-plus报 Invalid bound statement错误问题的解决方法

    2021-06-01 14:28:00
  • springboot如何读取自定义properties并注入到bean中

    2022-12-16 22:22:09
  • C++实现的链表类实例

    2023-07-04 08:36:36
  • Java AWT中常用的三种布局管理器详解

    2023-02-11 20:55:25
  • C# XML序列化方法及常用特性总结分析

    2021-12-27 02:23:33
  • Mybatis-Plus的使用详解

    2022-01-18 18:39:27
  • Netty分布式ByteBuf使用SocketChannel读取数据过程剖析

    2023-04-28 03:15:42
  • C语言字符串操作总结大全(超详细)

    2023-07-06 15:14:56
  • Java JDK动态代理(AOP)用法及实现原理详解

    2021-11-14 16:45:21
  • C# 中的多态底层虚方法调用详情

    2023-11-23 16:06:58
  • java poi设置生成的word的图片为上下型环绕以及其位置的实现

    2023-05-23 03:12:08
  • SpringBoot3.0自定stater模块的操作流程(chatGPT提供的49种场景)

    2023-06-15 05:21:46
  • Unity脚本自动添加头部注释的全过程

    2021-06-09 08:38:20
  • idea在用Mybatis时xml文件sql不提示解决办法(提示后背景颜色去除)

    2023-11-09 01:45:51
  • Java BigDecimal案例详解

    2021-09-15 12:03:43
  • asp之家 软件编程 m.aspxhome.com