news 2025/12/29 3:54:37

(35)使用Spring的AOP

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
(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();}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2025/12/25 21:59:36

(36)通知与切面

通知类型 通知类型包括&#xff1a; 前置通知&#xff1a;Before 目标方法执行之前的通知后置通知&#xff1a;AfterReturning 目标方法执行之后的通知环绕通知&#xff1a;Around 目标方法之前添加通知&#xff0c;同时目标方法执行之后添加通知。异常通知&#xff1a;AfterTh…

作者头像 李华
网站建设 2025/12/25 21:59:33

【鲲苍提效】一键批量接入外部应用监控,全面提升监控接入效率

汉得鲲苍基础架构管理平台的核心目标是为企业的异构系统提供简单高效的一站式统一闭环管理能力&#xff0c;包括统一资源&#xff08;集群、主机、存储等&#xff09;管理、统一应用及部署管理、统一监控管理、统一服务治理&#xff0c;帮助企业实现更快、更好、更全面的异构系…

作者头像 李华
网站建设 2025/12/27 19:34:52

4、索引有哪几种类型?

主键索引: 数据列不允许重复&#xff0c;不允许为NULL&#xff0c;一个表只能有一个主键。唯一索引: 数据列不允许重复&#xff0c;允许为NULL值&#xff0c;一个表允许多个列创建唯一索引。可以通过 ALTER TABLE table_name ADD UNIQUE (column); 创建唯一索引可以通过 ALTER …

作者头像 李华
网站建设 2025/12/25 21:30:17

一篇看懂JWT:Web安全的“身份证”

诸神缄默不语-个人技术博文与视频目录 文章目录 什么是JWT&#xff1f;一个简单的比喻为什么需要JWT&#xff1f;JWT长什么样&#xff1f;1. 头部&#xff08;Header&#xff09;2. 载荷&#xff08;Payload&#xff09;3. 签名&#xff08;Signature&#xff09; 用Python玩转…

作者头像 李华
网站建设 2025/12/25 21:27:31

基于微信小程序的直播带货商品数据分析系统毕设源码+文档+讲解视频

前言 本课题聚焦直播带货行业的数据化运营需求&#xff0c;针对当前直播商品数据分散、分析维度单一、运营决策缺乏精准数据支撑等痛点&#xff0c;设计开发基于微信小程序的直播带货商品数据分析系统。系统以微信小程序为核心载体&#xff0c;结合前端原生开发技术与后端数据处…

作者头像 李华
网站建设 2025/12/25 21:25:24

基于 S7 - 1200 和博图 15.1 的三层立体车库 PLC 设计

三层立体车库plc s7-1200 博图15.1 1、设置启动、停止按钮&#xff0c;且设置指示灯显示车库的开关状态&#xff1b; 2、7个车位的车俩可以自由存取&#xff0c;且车库可以实现自动存取&#xff08;存取选择最优路径&#xff09;&#xff1b; 3、每个车位均有电机控制&#…

作者头像 李华