广告
返回顶部
首页 > 资讯 > 后端开发 > Python >一文详解Spring AOP的配置与使用
  • 900
分享到

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

Spring AOP配置Spring AOP使用Spring AOP 2022-11-13 19:11:09 900人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录1.关于aop2.初步使用AOP环境配置3.使用原生spring api接口实现AOP4.使用自定义类实现AOP5.使用注解实现AOP1.关于AOP 面向切面编程(俗称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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 一文详解Spring AOP的配置与使用

本文链接: https://www.lsjlt.com/news/171004.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 一文详解Spring AOP的配置与使用
    目录1.关于AOP2.初步使用AOP环境配置3.使用原生Spring API接口实现AOP4.使用自定义类实现AOP5.使用注解实现AOP1.关于AOP 面向切面编程(俗称AOP)提...
    99+
    2022-11-13
    Spring AOP配置 Spring AOP使用 Spring AOP
  • 一篇文章带你详解Spring的AOP
    目录1、AOP 什么?2、需求3、解决办法1:使用静态代理第一步:创建 UserService 接口第二步:创建 UserService的实现类第三步:创建事务类 MyTransac...
    99+
    2022-11-13
  • 使用Spring Aop如何配置AspectJ注解
    这篇文章将为大家详细讲解有关使用Spring Aop如何配置AspectJ注解,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。采用的jdk代理,接口和实现类代码请参考上篇博文。主要是将Aspe...
    99+
    2023-05-31
    springaop aspectj
  • spring中aop的xml配置方法实例详解
    前言AOP:即面向切面编程,是一种编程思想,OOP的延续。在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等。aop,面向切面编程的目标就是分离关注点,比如:一个骑士只需要关注守护安全,或者远征,而骑士辉煌一生的事迹由谁...
    99+
    2023-05-31
    spring aop xml配置
  • 一篇文章从无到有详解Spring中的AOP
    前言 AOP (Aspect Orient Programming),直译过来就是 面向切面编程。AOP 是一种编程思想,是面向对象编程(OOP)的一种补充。面向对象编程将程序抽象成...
    99+
    2022-11-12
  • 带你了解Spring AOP的使用详解
    目录springmvc.xmlBankDaoAdminCheckBankDaoImplLogInfoTransmactionAdminCheckInterceptorLogInfoI...
    99+
    2022-11-12
  • 一文详解Spring是怎么读取配置Xml文件的
    目录Spring读取配置文件DocumentElementDocumentDefaultsDefinitionSpring读取配置文件Document 在XmlBeanDefinit...
    99+
    2022-11-13
  • Spring配置文件中parent与abstract的使用
    Spring配置文件parent与abstract 其实在基于spring框架开发的项目中,如果有多个bean都是一个类的实例,如配置多个数据源时,大部分配置的属性都一样,只有少部分...
    99+
    2022-11-12
  • 详解Spring配置及事务的使用
    目录 1.事务概念什么是事务?事务的四个特性(ACID):2.事务操作(模拟事务操作环境)3.事务管理(Spring事务管理)4.事务操作(注解声明式事务管理)在 spri...
    99+
    2022-11-12
  • 使用spring与hibernate实现配置文件
    这篇文章给大家介绍使用spring与hibernate实现配置文件,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。spring整合hibernate 有两种方式 1、注解方式 2、xml方式实现注解方式实现:applic...
    99+
    2023-05-31
    spring hibernate
  • 详解利用Spring加载Properties配置文件
    记得之前写Web项目的时候配置文件的读取都是用Properties这个类完成的,当时为了项目的代码的统一也就没做什么改动。但事后一直在琢磨SpringMVC会不会都配置的注解功能了?经过最近的研究返现SpringMVC确实带有这一项功能,S...
    99+
    2023-05-31
    spring 加载 properties
  • Spring中xml配置文件的基础使用方式详解
    目录1. xml配置文件的读取1.1 通过类路径读取配置文件1.2 通过文件系统绝对路径读取配置文件1.3使用BeanFactory接口读取配置文件2.带参构造对象的创建(const...
    99+
    2022-11-13
  • Spring配置数据源流程与作用详解
    目录一、数据源的作用二、数据源手动创建1、数据源的开发步骤2、手动创建c3p0数据源3、手动创建druid数据源4、通过properties配置文件创建连接池5、通过spring配置...
    99+
    2022-11-13
  • 一文详解Spring security框架的使用
    目录简介实例简介 Spring Security是一个基于Spring框架的安全认证和授权框架,它提供了一套全面的安全解决方案,可以在Web应用、移动应用和Web服务等不同场景下使用...
    99+
    2023-03-23
    Spring security框架使用 Spring security框架 Spring security
  • 详解Go语言中配置文件使用与日志配置
    目录项目结构调整配置文件使用日志配置小结接着上一篇的文章构建的项目:Go语学习笔记 - 环境安装、接口测试 只是简单的把GET和POST接口的使用测试了一下。 我还是想按照正常...
    99+
    2022-06-07
    详解go语言 GO 配置文件 go语言
  • Spring加载加密的配置文件详解
    本文实例为大家分享了Spring加载加密的配置文件,供大家参考,具体内容如下一、继承并实现自己的属性文件配置器类public class EncryptPropertyPlaceholderConfigurer extends Proper...
    99+
    2023-05-31
    spring 加载 加密
  • Spring实现文件上传的配置详解
    添加依赖 主要用来解析request请求流,获取文件字段名、上传文件名、content-type、headers等内容组装成FileItem <!--添加fil...
    99+
    2022-11-13
    Spring 文件上传 配置 Spring 文件上传
  • 详解Spring Bean的配置方式与实例化
    目录一、 Spring Bean 配置方式配置文件开发注解开发二、Spring Bean实例化环境准备构造方法实例化Bean静态工厂实例化Bean实例工厂实例化BeanFactory...
    99+
    2022-11-13
  • Spring使用注解和配置文件配置事务
    本文实例为大家分享了Spring使用注解和配置文件配置事务的具体代码,供大家参考,具体内容如下 需求图: 使用注解配置事务: package com.atguigu.spring...
    99+
    2022-11-13
  • 12-Composer的配置与使用详解
    1、自定义类与非类的自动加载与测试 # composer> php 包管理工具 ,类似npm1.自己写的类,函数,接口,常量等全局成员,通过自动加载来实现按需加载2.自己写的代码,有哪些依赖,用到了哪些外部成员,我自己知道,可以自己管理3....
    99+
    2023-09-05
    composer php 开发语言
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作