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

(35)使用Spring的AOP

Spring对AOP的实现包括以下3种方式:

  • 第一种方式:Spring框架结合AspectJ框架实现的AOP,基于注解方式。
  • 第二种方式:Spring框架结合AspectJ框架实现的AOP,基于XML方式。
  • 第三种方式:Spring框架自己实现的AOP,基于XML配置方式。

实际开发中,都是Spring+AspectJ来实现AOP。所以我们重点学习第一种和第二种方式。
什么是AspectJ?(Eclipse组织的一个支持AOP的框架。AspectJ框架是独立于Spring框架之外的一个框架,Spring框架用了AspectJ)
AspectJ项目起源于帕洛阿尔托(Palo Alto)研究中心(缩写为PARC)。该中心由Xerox集团资助,Gregor Kiczales领导,从1997年开始致力于AspectJ的开发,1998年第一次发布给外部用户,2001年发布1.0 release。为了推动AspectJ技术和社团的发展,PARC在2003年3月正式将AspectJ项目移交给了Eclipse组织,因为AspectJ的发展和受关注程度大大超出了PARC的预期,他们已经无力继续维持它的发展。

15.4.1 准备工作

使用Spring+AspectJ的AOP需要引入的依赖如下:

<!--spring context依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.0.0-M2</version></dependency><!--spring aop依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>6.0.0-M2</version></dependency><!--spring aspects依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>6.0.0-M2</version></dependency>

Spring配置文件中添加context命名空间和aop命名空间

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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"></beans>

15.4.2 基于AspectJ的AOP注解式开发

实现步骤

第一步:定义目标类以及目标方法

packagecom.powernode.spring6.service;// 目标类publicclassOrderService{// 目标方法publicvoidgenerate(){System.out.println("订单已生成!");}}

第二步:定义切面类

packagecom.powernode.spring6.service;importorg.aspectj.lang.annotation.Aspect;// 切面类@AspectpublicclassMyAspect{}

第三步:目标类和切面类都纳入spring bean管理
在目标类OrderService上添加**@Component注解。
在切面类MyAspect类上添加
@Component**注解。
第四步:在spring配置文件中添加组建扫描

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-scanbase-package="com.powernode.spring6.service"/></beans>

第五步:在切面类中添加通知

packagecom.powernode.spring6.service;importorg.springframework.stereotype.Component;importorg.aspectj.lang.annotation.Aspect;// 切面类@Aspect@ComponentpublicclassMyAspect{// 这就是需要增强的代码(通知)publicvoidadvice(){System.out.println("我是一个通知");}}

第六步:在通知上添加切点表达式

packagecom.powernode.spring6.service;importorg.aspectj.lang.annotation.Before;importorg.springframework.stereotype.Component;importorg.aspectj.lang.annotation.Aspect;// 切面类@Aspect@ComponentpublicclassMyAspect{// 切点表达式@Before("execution(* com.powernode.spring6.service.OrderService.*(..))")// 这就是需要增强的代码(通知)publicvoidadvice(){System.out.println("我是一个通知");}}

注解@Before表示前置通知。
第七步:在spring配置文件中启用自动代理

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-scanbase-package="com.powernode.spring6.service"/><!--开启自动代理--><aop:aspectj-autoproxyproxy-target-class="true"/></beans>

<aop:aspectj-autoproxy proxy-target-class=“true”/> 开启自动代理之后,凡事被代理的目标类(Target Class),即那些被切点表达式匹配到的、需要增强的 Bean都会生成代理对象。
proxy-target-class=“true” 表示采用cglib动态代理。
proxy-target-class=“false” 表示采用jdk动态代理。默认值是false。即使写成false,当没有接口的时候,也会自动选择cglib生成代理类。
测试程序:

packagecom.powernode.spring6.test;importcom.powernode.spring6.service.OrderService;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassAOPTest{@TestpublicvoidtestAOP(){ApplicationContextapplicationContext=newClassPathXmlApplicationContext("spring-aspectj-aop-annotation.xml");OrderServiceorderService=applicationContext.getBean("orderService",OrderService.class);orderService.generate();}}
http://www.jsqmd.com/news/139933/

相关文章:

  • YOLOv11 改进 - C2PSA | C2PSA融合TSSA(Token Statistics Self-Attention)令牌统计自注意力,优化遮挡目标感知
  • RabbitMQ vs RocketMQ ——延迟 / 定时消息落地终极指南
  • 科立干冰清洗机:研发实力、口碑售后解析 - 工业设备
  • (36)通知与切面
  • 外卖骑手实时就近派单全攻略:SpringBoot + GeoHash 高效实现
  • 我发现大文件HTTP上传阻塞 后来才知道用分块编码流式传输
  • 基于PSO-GA混合算法的施工进度计划多目标优化,以最小化总成本并实现资源均衡,满足工期约束和资源限制,MATLAB代码
  • Spring 7.0 与 Spring AI:Java 生态在 AI 时代的“绝对利器”
  • Java计算机毕设之基于VUE的旅游信息分享管理平台基于Springboot+Vue的旅游攻略分享平台系统(完整前后端代码+说明文档+LW,调试定制等)
  • (37)全注解式开发AOP
  • 揭开科立干冰清洗机神秘面纱:调试、能耗与研发能力解析 - 工业品网
  • 2025本科生必看!9个降AI率工具测评榜单
  • Java毕设项目:基于VUE的旅游信息分享管理平台(源码+文档,讲解、调试运行,定制等)
  • BMI160六轴惯性运动传感器原理图设计,已量产(加速度传感器)
  • 2025最新!专科生必看!8个AI论文平台测评,写毕业论文不再难
  • 微信小程序vue_uniapp研究生导师日常交互师生交流,考勤打卡任务,请假
  • 【鲲苍提效】一键批量接入外部应用监控,全面提升监控接入效率
  • BMM350三轴地磁传感器原理图设计,已量产(加速度传感器)
  • 4、索引有哪几种类型?
  • 从化房地产营销策划公司推荐:成本降低60%引爆热销潮 - 品牌测评家
  • PCIe-Tag字段与Outstanding Request
  • 海珠区心理咨询机构哪家好:权威榜单专业测评 - 品牌测评家
  • 实用指南:Redis底层数据结构 -- ziplist, quicklist, skiplist
  • hadoop 分布式集群启动命令 停止命令 hadoop jps查看每个节点状态命令
  • 基于贝叶斯优化的卷积神经网络-门控循环单元回归预测模型及评估指标 - BO-CNN-GRU B...
  • 探秘科立干冰清洗设备:高效靠谱之选 - 工业设备
  • 人工智能领域【专有名词汇总】...补充中...
  • 就想讨点学分有什么不队 - Beta冲刺
  • 科立干冰清洗机:,靠谱之选 - 工业品网
  • 不止溜背好看,这辆新奥迪还藏着“华为大脑”