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([权限修饰符] [返回类型] [类全路径] [方法名称]([参数列表]) )
AI 代码解读

例子

举例 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.*.* (..))
AI 代码解读

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......");
    }

}
AI 代码解读

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("环绕之后。。。。。。。");
    }

}
AI 代码解读

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>
AI 代码解读

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();

    }
}
AI 代码解读

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("环绕之后。。。。。。。");
    }

}
AI 代码解读

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

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

@Component
@Aspect
@Order(1)
public class UserProxy1
AI 代码解读

在这里插入图片描述
新建一个增强类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。。。。。。");
    }
}
AI 代码解读

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

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

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

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 {
}
AI 代码解读

5.2 测试方式改动

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

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

    }
AI 代码解读

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

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

6、后语

学无止境…

目录
打赏
0
0
0
0
218
分享
相关文章
Spring AI与DeepSeek实战二:打造企业级智能体
本文介绍如何基于Spring AI与DeepSeek模型构建企业级多语言翻译智能体。通过明确的Prompt设计,该智能体能自主执行复杂任务,如精准翻译32种ISO标准语言,并严格遵循输入格式和行为限制。代码示例展示了如何通过API实现动态Prompt生成和翻译功能,确保服务的安全性和可控性。项目已开源,提供更多细节和完整代码。 [GitHub](https://github.com/zlt2000/zlt-spring-ai-app) | [Gitee](https://gitee.com/zlt2000/zlt-spring-ai-app)
144 11
|
23天前
|
Spring AI与DeepSeek实战一:快速打造智能对话应用
在 AI 技术蓬勃发展的今天,国产大模型DeepSeek凭借其低成本高性能的特点,成为企业智能化转型的热门选择。而Spring AI作为 Java 生态的 AI 集成框架,通过统一API、简化配置等特性,让开发者无需深入底层即可快速调用各类 AI 服务。本文将手把手教你通过spring-ai集成DeepSeek接口实现普通对话与流式对话功能,助力你的Java应用轻松接入 AI 能力!虽然通过Spring AI能够快速完成DeepSeek大模型与。
360 11
微服务——SpringBoot使用归纳——Spring Boot中使用拦截器——拦截器使用实例
本文主要讲解了Spring Boot中拦截器的使用实例,包括判断用户是否登录和取消特定拦截操作两大场景。通过token验证实现登录状态检查,未登录则拦截请求;定义自定义注解@UnInterception实现灵活取消拦截功能。最后总结了拦截器的创建、配置及对静态资源的影响,并提供两种配置方式供选择,帮助读者掌握拦截器的实际应用。
18 0
|
5天前
|
微服务——SpringBoot使用归纳——Spring Boot中的切面AOP处理——Spring Boot 中的 AOP 处理
本文详细讲解了Spring Boot中的AOP(面向切面编程)处理方法。首先介绍如何引入AOP依赖,通过添加`spring-boot-starter-aop`实现。接着阐述了如何定义和实现AOP切面,包括常用注解如`@Aspect`、`@Pointcut`、`@Before`、`@After`、`@AfterReturning`和`@AfterThrowing`的使用场景与示例代码。通过这些注解,可以分别在方法执行前、后、返回时或抛出异常时插入自定义逻辑,从而实现功能增强或日志记录等操作。最后总结了AOP在实际项目中的重要作用,并提供了课程源码下载链接供进一步学习。
19 0
微服务——SpringBoot使用归纳——Spring Boot中的切面AOP处理——什么是AOP
本文介绍了Spring Boot中的切面AOP处理。AOP(Aspect Oriented Programming)即面向切面编程,其核心思想是分离关注点。通过AOP,程序可以将与业务逻辑无关的代码(如日志记录、事务管理等)从主要逻辑中抽离,交由专门的“仆人”处理,从而让开发者专注于核心任务。这种机制实现了模块间的灵活组合,使程序结构更加可配置、可扩展。文中以生活化比喻生动阐释了AOP的工作原理及其优势。
18 0
Jeesite5:Star24k,Spring Boot 3.3+Vue3实战开源项目,架构深度拆解!让企业级项目开发效率提升300%的秘密武器
Jeesite5 是一个基于 Spring Boot 3.3 和 Vue3 的企业级快速开发平台,集成了众多优秀开源项目,如 MyBatis Plus、Bootstrap、JQuery 等。它提供了模块化设计、权限管理、多数据库支持、代码生成器和国际化等功能,极大地提高了企业级项目的开发效率。Jeesite5 广泛应用于企业管理系统、电商平台、客户关系管理和知识管理等领域。通过其强大的功能和灵活性,Jeesite5 成为了企业级开发的首选框架之一。访问 [Gitee 页面](https://gitee.com/thinkgem/jeesite5) 获取更多信息。
Jeesite5:Star24k,Spring Boot 3.3+Vue3实战开源项目,架构深度拆解!让企业级项目开发效率提升300%的秘密武器
Spring中使用AspectJ实现AOP
一,一些基本概念                Spring除了IOC容器之外,另一大核心就是AOP了。Spring 中AOP是通过AspectJ来实现的。                   首先来看下AOP 的相关概念:        1,Aspect                     对横切性关注点的模块化。
1224 0
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
24 0
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 介绍
本课介绍Spring Boot集成Thymeleaf模板引擎。Thymeleaf是一款现代服务器端Java模板引擎,支持Web和独立环境,可实现自然模板开发,便于团队协作。与传统JSP不同,Thymeleaf模板可以直接在浏览器中打开,方便前端人员查看静态原型。通过在HTML标签中添加扩展属性(如`th:text`),Thymeleaf能够在服务运行时动态替换内容,展示数据库中的数据,同时兼容静态页面展示,为开发带来灵活性和便利性。
24 0
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
本课主要讲解Spring Boot项目中的属性配置方法。在实际开发中,测试与生产环境的配置往往不同,因此不应将配置信息硬编码在代码中,而应使用配置文件管理,如`application.yml`。例如,在微服务架构下,可通过配置文件设置调用其他服务的地址(如订单服务端口8002),并利用`@Value`注解在代码中读取这些配置值。这种方式使项目更灵活,便于后续修改和维护。
14 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等