一文详解Spring AOP的配置与使用

 更新时间:2022年11月08日 11:03:54   作者:世界尽头与你  
面向切面编程(俗称AOP)提供了一种面向对象编程(俗称OOP)的补充,面向对象编程最核心的单元是类(class),然而面向切面编程最核心的单元是切面(Aspects)。本文就来和大家聊聊AOP的配置与使用,感兴趣的可以了解一下

1.关于AOP

面向切面编程(俗称AOP)提供了一种面向对象编程(俗称OOP)的补充,面向对象编程最核心的单元是类(class),然而面向切面编程最核心的单元是切面(Aspects)。与面向对象的顺序流程不同,AOP采用的是横向切面的方式,注入与主业务流程无关的功能,例如事务管理和日志管理。

图示:

Spring的一个关键组件是AOP框架。 虽然Spring IoC容器不依赖于AOP(意味着你不需要在IOC中依赖AOP),但AOP为Spring IoC提供了非常强大的中间件解决方案。

AOP 是一种编程范式,最早由 AOP 联盟的组织提出的,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。它是 OOP的延续。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率

2.初步使用AOP环境配置

要使用Spring AOP,需要导入如下的maven包:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.9.1</version>
</dependency>

在对应的Spring配置文件中,需要导入aop的约束:

xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd"

整体的配置如下:

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

​​​​​​​</beans>

编写接口类:UserService.java

public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void query();
}

接口实现类:UserServiceImpl.java

public class UserServiceImpl implements UserService {

    @Override
    public void add() {
        System.out.println("增加用户");
    }

    @Override
    public void delete() {
        System.out.println("删除用户");
    }

    @Override
    public void update() {
        System.out.println("更新用户");
    }

    @Override
    public void query() {
        System.out.println("查找用户");
    }
}

待插入的前置日志类:Log.java

/**
 * 插入的前置日志类
 */
public class Log implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了!");
    }
}

待插入的后置日志类:AfterLog.java

/**
 * 插入的后置日志类
 */
public class AfterLog implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了" + method.getName() + "方法,返回结果为:" + returnValue);
    }
}

注册类的bean标签:

<!-- 注册测试bean -->
<bean id="userService" class="top.imustctf.service.UserServiceImpl"/>
<bean id="log" class="top.imustctf.log.Log"/>
<bean id="afterLog" class="top.imustctf.log.AfterLog"/>

3.使用原生Spring API接口实现AOP

配置aop:

切入点是待切入的方法,使用正则表达式匹配

执行环绕增加是具体向切入点添加日志的配置

<!-- 配置AOP -->
<aop:config>
    <!-- 配置切入点 -->
    <aop:pointcut id="pointcut" expression="execution(* top.imustctf.service.UserServiceImpl.*(..))"/>
    <!-- 执行环绕增加 -->
    <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
    <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>

现在来测试一下吧:

可以看到,AOP动态代理切入成功了!

@Test
public void test() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = context.getBean("userService", UserService.class);
    userService.add();
    // top.imustctf.service.UserServiceImpl的add被执行了!
    // 增加用户
    // 执行了add方法,返回结果为:null
}

4.使用自定义类实现AOP

先Diy一个切面类:DiyPointCut.java

public class DiyPointCut {
    public void before() {
        System.out.println("方法执行前");
    }

    public void after() {
        System.out.println("方法执行后");
    }
}

注册diy类并配置切面:

<bean id="diy" class="top.imustctf.diy.DiyPointCut"/>
<aop:config>
    <!-- 定义一个切面,ref中为要引用的类对象 -->
    <aop:aspect ref="diy">
        <!-- 配置切入点 -->
        <aop:pointcut id="point" expression="execution(* top.imustctf.service.UserServiceImpl.*(..))"/>
        <!-- 通知 -->
        <aop:before method="before" pointcut-ref="point"/>
        <aop:after method="after" pointcut-ref="point"/>
    </aop:aspect>
</aop:config>

来开始测试:

@Test
public void test() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = context.getBean("userService", UserService.class);
    userService.add();
    // 方法执行前
    // 增加用户
    // 方法执行后
}

5.使用注解实现AOP

使用注解实现AOP,它更简单,更强大!

在使用注解开发前,需要在Spring配置文件中开启动态代理的支持:

<aop:aspectj-autoproxy/>

接下来,使用注解直接开发AOP类:

@Component
@Aspect  // 标注这个类是一个切面
public class AnnotationPointCut {
    @Before("execution(* top.imustctf.service.UserServiceImpl.*(..))")
    public void before() {
        System.out.println("方法执行前啊!");
    }

    @After("execution(* top.imustctf.service.UserServiceImpl.*(..))")
    public void after() {
        System.out.println("方法执行后啊!");
    }
}

现在来测试一下吧:

@Test
public void test() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = context.getBean("userService", UserService.class);
    userService.add();
    // 方法执行前啊!
    // 增加用户
    // 方法执行后啊!
}

到此这篇关于一文详解Spring AOP的配置与使用的文章就介绍到这了,更多相关Spring AOP内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Mybatis逆向工程笔记小结

    Mybatis逆向工程笔记小结

    MyBatis官方为我们提供了一个逆向工程,通过这个逆向工程,只需要建立好数据表,MyBatis就会根据这个表自动生成pojo类、mapper接口、sql映射文件,本文主要介绍了Mybatis逆向工程笔记小结,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • Java中内存分配的几种方法

    Java中内存分配的几种方法

    本文主要介绍Java中几种分配内存的方法。我们会看到如何使用sun.misc.Unsafe来统一操作任意类型的内存。以前用C语言开发的同学通常都希望能在Java中通过较底层的接口来操作内存,他们一定会对本文中要讲的内容感兴趣
    2014-03-03
  • Java字符编码解码的实现详解

    Java字符编码解码的实现详解

    本篇文章介绍了,Java字符编码解码的实现详解。需要的朋友参考下
    2013-05-05
  • springcloud之FeignClient使用详解

    springcloud之FeignClient使用详解

    Feign是一种声明式、模板化的HTTP客户端,可以简化微服务之间的远程过程调用,通过Feign,开发者可以像调用本地方法一样调用远程服务,而无需感知这是一个HTTP请求,引入Feign后,微服务之间的调用流程更加简化,结合Ribbon实现了路由负载、超时熔断等功能
    2024-12-12
  • Java将时间按月份分段的实现思路与方法

    Java将时间按月份分段的实现思路与方法

    这篇文章主要给大家介绍了关于Java将时间按月份分段的实现思路与方法,通过文中介绍的方法可以将时间分成我们想要的时间段,文中给出了详细的实例代码,需要的朋友可以参考下
    2021-07-07
  • IntelliJ IDEA 刷题利器 LeetCode 插件详解

    IntelliJ IDEA 刷题利器 LeetCode 插件详解

    这篇文章主要介绍了IntelliJ IDEA 刷题利器 LeetCode 插件,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • Netty核心功能之数据容器ByteBuf详解

    Netty核心功能之数据容器ByteBuf详解

    这篇文章主要为大家介绍了Netty核心功能之数据容器ByteBuf详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Jmeter线程组传参原理解析

    Jmeter线程组传参原理解析

    这篇文章主要介绍了jmeter线程组传参原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • Java tomcat环境变量及idea配置解析

    Java tomcat环境变量及idea配置解析

    这篇文章主要介绍了Java tomcat环境变量及idea配置解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-12-12
  • Java HashMap实现原理分析(一)

    Java HashMap实现原理分析(一)

    这篇文章主要介绍了Java HashMap实现原理的分析,帮助大家更好的理解和使用Java,感兴趣的朋友可以了解下
    2020-08-08

最新评论