Spring5入门到实战------10、操作术语解释--Aspectj注解开发实例。AOP切面编程的实际应用

简介: 这篇文章是Spring5框架的实战教程,详细解释了AOP的关键术语,包括连接点、切入点、通知、切面,并展示了如何使用AspectJ注解来开发AOP实例,包括切入点表达式的编写、增强方法的配置、代理对象的创建和优先级设置,以及如何通过注解方式实现完全的AOP配置。

1、操作术语

1.1、连接点

类里面哪些方法可以被增强、这些方法被称为连接点。比如:用户控制层有登录、注册、修改密码、修改信息等方法。假如只有登录类和注册类可以被增强,登录和注册方法就称为连接点

1.2、切入点

实际被真正增强的方法,称为切入点。假如登录方法被正真增强(登陆前做些权限验证之类的、假设原始方法只是查询数据库、无权限认证过程)、登录方法又称为切入点。

1.3、通知(增强)

实际增强的逻辑部分称为通知(增强)。你编写的新的业务逻辑、比如在登录前进行的权限认证操作。

通知有多种类型

  • 前置通知
  • 后置通知
  • 环绕通知
  • 异常通知
  • 最终通知

1.4、切面

把通知应用到切入点过程。你编写的业务逻辑(通知)如何加入到之前的方法(切入点)

2、准备工作和如何使用

友情提示:如果直接建立spring项目、则不需要进行这一步
2.1

2.1 jar包引入

1、Spring 框架一般都是基于 AspectJ 实现 AOP 操作

  • AspectJ 不是 Spring 组成部分,独立 AOP 框架,一般把 AspectJ 和 Spirng 框架一起使用,进行 AOP 操作

2、基于 AspectJ 实现 AOP 操作

  • 基于 xml 配置文件实现
  • 基于注解方式实现(使用)

3、在项目工程里面引入 AOP 相关依赖
在这里插入图片描述

2.2、切入点表达式(具体使用)

(1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强
(2)语法结构: execution([权限修饰符] [返回类型] [类全路径] [方法名称]([参数列表]) )

例子

举例 1:对 com.zyz.dao.BookDao 类里面的 add 进行增强
execution(* com.zyz.dao.BookDao.add(..))

举例 2:对 com.zyz.dao.BookDao 类里面的所有的方法进行增强
execution(* com.zyz.dao.BookDao.* (..))

举例 3:对 com.zyz.dao 包里面所有类,类里面所有方法进行增强
execution(* com.zyz.dao.*.* (..))

3、代码实战

3.1 User .java

一个类里边的基本方法。 使用注解@Component创建 User 对象。

/**
 * @author Lenovo
 * @version 1.0
 * @data 2022/10/20 22:16
 */
@Component
public class User {
    public void add(){
//        int a = 1/0;
        System.out.println("add......");
    }

}

3.2 UserProxy .java

1、代理类中进行方法的增强。
2、使用注解@Component创建 UserProxy 对象。
3、在增强类上面添加注解 @Aspec。
4、增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置

/**
 * 增强的类
 * @author Lenovo
 * @version 1.0
 * @data 2022/10/20 22:19
 */
@Component
@Aspect//生成代理对象
public class UserProxy {

    /**
     * 1、前置通知
     */
    @Before(value = "execution(* com.zyz.spring5.aop.User.add(..))")
    public void before(){
        System.out.println("before。。。。。。");
    }

    /**
     * 2、后置通知
     */
    @AfterReturning(value = "execution(* com.zyz.spring5.aop.User.add(..))")
    public void afterReturnning(){
        System.out.println("afterReturnning。。。。。。");
    }

    /**
     * 3、最终通知
     */
    @After(value = "execution(* com.zyz.spring5.aop.User.add(..))")
    public void after(){
        System.out.println("after。。。。。。");
    }

    /**
     * 4、异常通知
     */
    @AfterThrowing(value = "execution(* com.zyz.spring5.aop.User.add(..))")
    public void afterThrowing(){
        System.out.println("afterThrowing。。。。。。");
    }

    /**
     * 5、环绕通知
     * @param proceedingJoinPoint
     * @throws Throwable
     */
    @Around(value = "execution(* com.zyz.spring5.aop.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前。。。。。。。");
        //被增强的方法执行
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后。。。。。。。");
    }

}

3.3 bean.xml

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

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.zyz.spring5.aop"></context:component-scan>

    <!-- 开启 Aspect 生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

3.4 测试类

/**
 * @author Lenovo
 * @version 1.0
 * @data 2022/10/20 22:38
 */
public class Test {

    @org.junit.Test
    public void testDemo(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        User user = context.getBean("user", User.class);
        user.add();

    }
}

3.5 测试结果

在这里插入图片描述
在这里插入图片描述


4、优化代码

4.1 相同的切入点抽取

仔细看代码不难发现、耦合性很高。比如增强的方法类的位置移动。那么所有增强的表达式中的路径也要一个一个改动(3.2 UserProxy.java)

相同的切入点抽取、达到复用的效果。可以只需要改动少量的代码、完成相同的事情。便于后期的维护

/**
 * 增强的类
 * @author Lenovo
 * @version 1.0
 * @data 2022/10/20 22:19
 */
@Component
@Aspect//生成代理对象
public class UserProxy {
    /**
     * 相同切入点抽取
     */
    @Pointcut(value = "execution(* com.zyz.spring5.aop.User.add(..))")
    public void pointDemo(){}

    /**
     * 1、前置通知
     */
    @Before(value = "pointDemo()")
    public void before(){
        System.out.println("before。。。。。。");
    }

    /**
     * 2、后置通知
     */
    @AfterReturning(value = "pointDemo()")
    public void afterReturnning(){
        System.out.println("afterReturnning。。。。。。");
    }

    /**
     * 3、最终通知
     */
    @After(value = "pointDemo()")
    public void after(){
        System.out.println("after。。。。。。");
    }

    /**
     * 4、异常通知
     */
    @AfterThrowing(value = "pointDemo()")
    public void afterThrowing(){
        System.out.println("afterThrowing。。。。。。");
    }

    /**
     * 5、环绕通知
     * @param proceedingJoinPoint
     * @throws Throwable
     */
    @Around(value = "pointDemo()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕之前。。。。。。。");
        //被增强的方法执行
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后。。。。。。。");
    }

}

4.2 有多个增强类多同一个方法进行增强,设置增强类优先级

@Order(1)数字越小优先级越高

@Component
@Aspect
@Order(1)
public class UserProxy1

在这里插入图片描述
新建一个增强类1

/**
 * @author Lenovo
 * @version 1.0
 * @data 2022/10/22 18:50
 */
@Component
@Aspect
@Order(1)
public class UserProxy1 {

    /**
     * 1、前置通知
     */
    @Before(value = "execution(* com.zyz.spring5.aop.User.add(..))")
    public void before(){
        System.out.println("我的优先级高哦、我先执行。before。。。。。。");
    }
}

之前的增强类也添加一个优先级

@Component
@Aspect//生成代理对象
@Order(3)
public class UserProxy {

测试结果
在这里插入图片描述

5、完全注解开发

5.1 新增一个配置类

ConfigAop.java

/**
 * @author Lenovo
 * @version 1.0
 * @data 2022/10/22 18:58
 */
@Configuration
@ComponentScan(basePackages = {"com.zyz"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ConfigAop {
}

5.2 测试方式改动

之前读取的是配置文件。现在要读取配置类

    @org.junit.Test
    public void testDemo1(){
        //加载配置类
        ApplicationContext context = new AnnotationConfigApplicationContext(ConfigAop.class);
        User user = context.getBean("user", User.class);
        user.add();

    }

测试结果不变
在这里插入图片描述

目录结构
在这里插入图片描述

6、后语

学无止境…

相关文章
|
2月前
|
人工智能 Java API
Spring AI 实战|Spring AI入门之DeepSeek调用
本文介绍了Spring AI框架如何帮助Java开发者轻松集成和使用大模型API。文章从Spring AI的初探开始,探讨了其核心能力及应用场景,包括手动与自动发起请求、流式响应实现打字机效果,以及兼容不同AI服务(如DeepSeek、通义千问)的方法。同时,还详细讲解了如何在生产环境中添加监控以优化性能和成本管理。通过Spring AI,开发者可以简化大模型调用流程,降低复杂度,为企业智能应用开发提供强大支持。最后,文章展望了Spring AI在未来AI时代的重要作用,鼓励开发者积极拥抱这一技术变革。
731 71
Spring AI 实战|Spring AI入门之DeepSeek调用
|
17天前
|
人工智能 缓存 监控
构建企业级AI智能体(Spring AI Alibaba + JManus实战)
本内容深入探讨了基于Spring AI Alibaba 1.2 + JManus 0.9 + DeepSeek-V3等技术栈构建的电商客服工单智能处理系统。通过分析传统AI应用的三重困境,介绍了JManus智能体架构的核心突破,包括动态任务分解、状态持久化和服务热插拔等优势。同时详细展示了企业级环境配置、智能体行为设计及多智能体协作全流程,并提供了性能优化、生产部署与监控方案。最后,结合压力测试结果和调优策略,总结了企业级智能体设计原则与未来演进方向,为实现从“AI试验”到“AI生产”的转变提供了实践指导。
289 13
|
24天前
|
Cloud Native Java 微服务
Spring Boot 3.x 现代化应用开发实战技巧与最佳实践
本指南基于Spring Boot 3.x,融合微服务、云原生与响应式编程等前沿技术,打造现代化应用开发实践。通过构建智能电商平台案例,涵盖商品、订单、用户等核心服务,展示Spring WebFlux、OAuth 2.0认证、Spring Cloud Gateway路由、GraalVM原生编译等技术实现。同时提供Docker/Kubernetes部署方案及性能优化策略,助您掌握从开发到生产的全流程。代码示例详实,适合进阶开发者参考。
99 2
|
18天前
|
Java API 微服务
Java 21 与 Spring Boot 3.2 微服务开发从入门到精通实操指南
《Java 21与Spring Boot 3.2微服务开发实践》摘要: 本文基于Java 21和Spring Boot 3.2最新特性,通过完整代码示例展示了微服务开发全流程。主要内容包括:1) 使用Spring Initializr初始化项目,集成Web、JPA、H2等组件;2) 配置虚拟线程支持高并发;3) 采用记录类优化DTO设计;4) 实现JPA Repository与Stream API数据访问;5) 服务层整合虚拟线程异步处理和结构化并发;6) 构建RESTful API并使用Springdoc生成文档。文中特别演示了虚拟线程配置(@Async)和StructuredTaskSco
69 0
|
安全 Java Spring
Spring之Aop的底层原理
Spring之Aop的底层原理
|
设计模式 Java uml
Spring AOP 原理
Spring AOP 原理
64 0
|
Java Spring 容器
【Spring AOP底层实现原理】
【Spring AOP底层实现原理】
284 0
|
11月前
|
Java
Spring5入门到实战------9、AOP基本概念、底层原理、JDK动态代理实现
这篇文章是Spring5框架的实战教程,深入讲解了AOP的基本概念、如何利用动态代理实现AOP,特别是通过JDK动态代理机制在不修改源代码的情况下为业务逻辑添加新功能,降低代码耦合度,并通过具体代码示例演示了JDK动态代理的实现过程。
Spring5入门到实战------9、AOP基本概念、底层原理、JDK动态代理实现
|
8月前
|
Java 开发者 Spring
Spring AOP 底层原理技术分享
Spring AOP(面向切面编程)是Spring框架中一个强大的功能,它允许开发者在不修改业务逻辑代码的情况下,增加额外的功能,如日志记录、事务管理等。本文将深入探讨Spring AOP的底层原理,包括其核心概念、实现方式以及如何与Spring框架协同工作。