当前位置: 首页 > article >正文

AOP 面向切片编程

目录

1. 什么是AOP

2. AOP的应用场景

3. AOP在Java中的实现

4. Spring AOP概述

5. Spring AOP的配置

1.在pom.xml文件中添加Spring AOP的依赖:

2. 定义切面

3. 启用AOP

4. 目标类

5. 测试AOP

6. AOP与其他技术的对比

面向切面编程(AOP, Aspect-Oriented Programming)是一种编程范式,旨在通过分离横切关注点来提高代码的模块化。AOP的核心思想是将那些跨越多个模块的通用行为(如日志记录、事务管理、安全检查等)从业务逻辑代码中分离出来,以提高代码的可维护性和可重用性。本文将详细介绍Java中的AOP概念,并通过代码示例展示如何在实际项目中应用AOP。

1. 什么是AOP

AOP是一种编程技术,用于在应用程序中动态地将代码切片(如日志、事务等)插入到特定位置。AOP的核心概念包括:

切面(Aspect):切面是模块化的关注点,通常是横切关注点,如日志记录、事务管理等。
连接点(Join Point):连接点是在程序执行过程中某个特定的点,如方法调用或异常抛出。
通知(Advice):通知是切面在连接点上执行的代码。
切入点(Pointcut):切入点是一个表达式,用于匹配连接点。
引入(Introduction):引入允许在不修改现有类的情况下向现有类添加新方法或属性。
织入(Weaving):织入是将切面应用到目标对象并创建代理对象的过程。


2. AOP的应用场景

AOP非常适用于处理那些横切关注点,即那些在多个模块中重复出现代码逻辑。典型的应用场景包括:

日志记录:在方法调用之前和之后记录日志。
事务管理:在方法调用开始前开启事务,方法调用结束后提交事务,方法调用异常时回滚事务。
安全检查:在方法调用前进行权限验证。
性能监控:监控方法的执行时间。


3. AOP在Java中的实现

在Java中,实现AOP的常用框架有AspectJSpring AOP。本文主要介绍Spring AOP的实现。

4. Spring AOP概述

Spring AOP是Spring框架中的一个模块,提供了对AOP的支持。Spring AOP基于代理模式,通过在运行时生成代理对象来实现AOP功能。Spring AOP支持以下类型的通知:

前置通知(Before Advice):在目标方法执行之前执行。
后置通知(After Advice):在目标方法执行之后执行。
返回通知(After Returning Advice):在目标方法成功返回之后执行。
异常通知(After Throwing Advice):在目标方法抛出异常之后执行。
环绕通知(Around Advice):包裹目标方法的执行,在目标方法执行之前和之后都可以执行。


5. Spring AOP的配置

Spring AOP的配置可以使用XML或注解方式。以下是使用注解配置AOP的示例。

1.在pom.xml文件中添加Spring AOP的依赖:

<dependencies>
    <!-- Spring AOP -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <!-- 其他依赖项 -->
</dependencies>

 

2. 定义切面

接下来,定义一个切面类,包含切入点和通知:

package com.example.aopdemo.aspect;
 
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
 
@Aspect
@Component
public class LoggingAspect {
 
    @Before("execution(* com.example.aopdemo.service.*.*(..))")
    public void logBeforeMethod() {
        System.out.println("Method execution started...");
    }
}

在上述代码中,我们定义了一个切面类LoggingAspect,并使用@Aspect注解标记它。@Before注解定义了一个前置通知,表示在com.example.aopdemo.service包下的所有类的所有方法执行之前,执行logBeforeMethod方法。

3. 启用AOP

在Spring Boot应用的主类中,使用@EnableAspectJAutoProxy注解启用AOP:

package com.example.aopdemo;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
@SpringBootApplication
@EnableAspectJAutoProxy
public class AopDemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(AopDemoApplication.class, args);
    }
}

 

4. 目标类

定义一个目标类和方法,以便演示AOP通知的应用:

package com.example.aopdemo.service;
 
import org.springframework.stereotype.Service;
 
@Service
public class UserService {
 
    public void createUser() {
        System.out.println("Creating user...");
    }
}

 

5. 测试AOP

编写一个控制器或测试类来触发目标方法:

package com.example.aopdemo.controller;
 
import com.example.aopdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @GetMapping("/createUser")
    public String createUser() {
        userService.createUser();
        return "User creation initiated";
    }
}package com.example.aopdemo.controller;
 
import com.example.aopdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @GetMapping("/createUser")
    public String createUser() {
        userService.createUser();
        return "User creation initiated";
    }
}package com.example.aopdemo.controller;
 
import com.example.aopdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @GetMapping("/createUser")
    public String createUser() {
        userService.createUser();
        return "User creation initiated";
    }
}package com.example.aopdemo.controller;
 
import com.example.aopdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @GetMapping("/createUser")
    public String createUser() {
        userService.createUser();
        return "User creation initiated";
    }
}

 启动应用程序并访问/createUser端点,您将看到以下输出:

Method execution started...
Creating user... 

通过上述步骤,我们已经成功地使用Spring AOP实现了日志记录的功能。在目标方法执行之前,日志记录通知被触发并执行。

6. AOP与其他技术的对比

 

结论


面向切面编程(AOP)为Java开发者提供了一种强大的工具来处理横切关注点,使代码更加模块化和可维护。通过Spring AOP,我们可以轻松地将日志记录、事务管理、安全检查等功能分离出来,集中管理。这不仅提高了代码的可读性和可维护性,也使得我们的应用程序更加灵活和健壮。

AOP虽然强大,但也需要谨慎使用。在设计和实现AOP时,应仔细考虑其对代码复杂性和性能的影响,以确保其带来的好处大于其引入的开销。

 


http://www.kler.cn/news/284955.html

相关文章:

  • 我的推荐:腾讯云罗云《从零构建向量数据库》
  • 无人机之遥控器防水性能篇
  • Ubuntu 20.04 安装 GitHub CLI(gh),并使用
  • C语言——简单的do while循环找100~999之间的水仙花数(所有的三位水仙花数)
  • 数据结构(三)——双向链表,循环链表,内核链表,栈和队列
  • 『功能项目』怪物反击主角复活【14】
  • spring security 会话管理
  • 苹果M4芯片Mac全面曝光 或10月发布
  • OpenHarmony轻量设备Hi3861芯片开发板启动流程分析
  • redis能正常访问,但是springboot编译报错
  • 【Go函数详解】二、参数传递、变长参数与多返回值
  • java定时服务
  • Python学习日志(1)——安装
  • Linux-arm64中断现场保护详解
  • MySQL 集群技术全攻略:从搭建到优化(上)
  • 分类模型评估指标——准确率、精准率、召回率、F1、ROC曲线、AUC曲线
  • 快递盒检测检测系统源码分享 # [一条龙教学YOLOV8标注好的数据集一键训练_70+全套改进创新点发刊_Web前端展示]
  • RAG 向量数据库:掌握 Elasticsearch 作为向量数据库的终极指南
  • 【Python零基础】文件使用和异常处理
  • Vue(四) 组件、单文件组件、非单文件组件,重要的内置关系
  • 【计组 | Cache原理】讲透Cache的所有概念与题型方法
  • 大模型好书案例——《BERT基础教程:Transformer大模型实战》(附PDF)
  • LuaJit分析(一)LuaJit交叉编译
  • TCP的连接与断开
  • java基础开发-xstream解析xml
  • 去中心化(Decentralization)
  • leetcode1514 最大概率路径(Bellman-ford算法详解)
  • 栈算法【基于顺序表】
  • centos 系统yum 安装 mariadb
  • UML类图中的组合关系