Spring3系列12-Spring AOP AspectJ

本文讲述使用AspectJ框架实现Spring AOP。

再重复一下Spring AOP中的三个概念,

  1. Advice:向程序内部注入的代码。
  2. Pointcut:注入Advice的位置,切入点,一般为某方法。
  3. Advisor:Advice和Pointcut的结合单元,以便将Advice和Pointcut分开实现灵活配置。

AspectJ是基于注释(Annotation)的,所以需要JDK5.0以上的支持。

AspectJ支持的注释类型如下:

  1. @Before
  2. @After
  3. @AfterReturning
  4. @AfterThrowing
  5. @Around

 

首先定义一个简单的bean,CustomerBo实现了接口ICustomerBo

ICustomerBo.java如下:

package com.lei.demo.aop.aspectj;public interface ICustomerBo {void addCustomer();void deleteCustomer();String AddCustomerReturnValue();void addCustomerThrowException() throws Exception;void addCustomerAround(String name);}

 

 CustomerBo.java如下:

 

package com.lei.demo.aop.aspectj;public class CustomerBo implements ICustomerBo {public void addCustomer() {System.out.println("addCustomer() is running ...");}public void deleteCustomer() {System.out.println("deleteCustomer() is running ...");}public String AddCustomerReturnValue() {System.out.println("AddCustomerReturnValue() is running ...");return "abc";}public void addCustomerThrowException() throws Exception {System.out.println("addCustomerThrowException() is running ...");throw new Exception("Generic Error");}public void addCustomerAround(String name) {System.out.println("addCustomerAround() is running ,args:"+name);}}

 

一、      简单的AspectJ,Advice和Pointcut结合在一起

首先没有引入Pointcut之前,Advice和Pointcut是混在一起的

步骤,只需要两步,如下:

  1. 创建一个Aspect类
  2. 配置Spring配置文件

 

第一步,创建Aspect类

LoggingAspect.java如下:

package com.lei.demo.aop.aspectj;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;@Aspect
public class LoggingAspect {@Before("execution(public * com.lei.demo.aop.aspectj.CustomerBo.addCustomer(..))")public void logBefore(JoinPoint joinPoint){System.out.println("logBefore() is running ...");System.out.println("hijacked:"+joinPoint.getSignature().getName());System.out.println("**********");}@After("execution(public * com.lei.demo.aop.aspectj.CustomerBo.deleteCustomer(..))")public void logAfter(JoinPoint joinPoint){System.out.println("logAfter() is running ...");System.out.println("hijacked:"+joinPoint.getSignature().getName());System.out.println("**********");}
}

 

解释:

1.  必须使用@AspectLoggingAspect声明之前注释,以便被框架扫描到

2.  此例AdvicePointcut结合在一起,类中的具体方法logBeforelogAfter即为Advice,是要注入的代码,Advice方法上的表达式为Pointcut表达式,即定义了切入点,上例中@Before注释的表达式代表执行CustomerBo.addCustomer方法时注入logBefore代码。

3.  LoggingAspect方法上加入@Before或者@After等注释

4.  "execution(public * com.lei.demo.aop.aspectj.CustomerBo.addCustomer(..))"Aspect的切入点表达式,其中,*代表返回类型,后边的就要定义要拦截的方法名,这里写的的是com.lei.demo.aop.aspectj.CustomerBo.addCustomer表示拦截CustomerBo中的addCustomer方法,(..)代表参数匹配,此处表示匹配任意数量的参数,可以是0个也可以是多个,如果你确定这个方法不需要使用参数可以直接用(),还可以使用(*)来匹配一个任意类型的参数,还可以使用 (* , String),这样代表匹配两个参数,第二个参数必须是String 类型的参数

5.  AspectJ表达式,可以对整个包定义,例如,execution(* com.lei.service..*.*(..))表示切入点是com.lei.sevice包中的任意一个类的任意方法,具体的表达式请自行百度。

 

第二步,配置Spring配置文件,

配置Spring-AOP-AspectJ.xml文件,如下:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><aop:aspectj-autoproxy/><bean id="customerBo" class="com.lei.demo.aop.aspectj.CustomerBo"/><bean id="logAspect" class="com.lei.demo.aop.aspectj.LoggingAspect" /></beans>

 

解释:

1.      <aop:aspectj-autoproxy/>启动AspectJ支持,这样Spring会自动寻找用@Aspect注释过的类,其他的配置与spring普通bean配置一样。

 

测试:

执行App.java如下:

package com.lei.demo.aop.aspectj;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main(String[] args) {ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] { "Spring-AOP-AspectJ.xml" });ICustomerBo customer=(ICustomerBo)appContext.getBean("customerBo");customer.addCustomer();System.out.println("-------------------------------------------");customer.deleteCustomer();}
}

 

结果:

logBefore() is running ...

hijacked:addCustomer

**********

addCustomer() is running ...

-------------------------------------------

deleteCustomer() is running ...

logAfter() is running ...

hijacked:deleteCustomer

**********

 

二、      将Advice和Pointcut分开

需要三步,

  1. 创建Pointcut
  2. 创建Advice
  3. 配置Spring的配置文件

 

第一步,PointcutsDefinition.java定义了Pointcut,如下:

package com.lei.demo.aop.aspectj;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;@Aspect
public class PointcutsDefinition {@Pointcut("execution(* com.lei.demo.aop.aspectj.CustomerBo.*(..))")public void customerLog() {}
}

 

解释:

1. 类声明前加入@Aspect注释,以便被框架扫描到。

2. @Pointcut是切入点声明,指定需要注入的代码的位置,如上例中指定切入点为CustomerBo类中的所有方法,在实际业务中往往是指定切入点到一个逻辑层,例如 execution (* com.lei.business.service.*.*(..)),表示aop切入点为service包中所有类的所有方法,具体的表达式后边会有介绍。

3. 方法customerLog是一个签名,在Advice中可以用此签名代替切入点表达式,所以不需要在方法体内编写实际代码,只起到助记功能,例如此处代表操作CustomerBo类时需要的切入点。

 

第二步,创建Advice类

LoggingAspect.java如下:

package com.lei.demo.aop.aspectj;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;@Aspect
public class LoggingAspect {@Before("com.lei.demo.aop.aspectj.PointcutsDefinition.customerLog()")public void logBefore(JoinPoint joinPoint){System.out.println("logBefore() is running ...");System.out.println("hijacked:"+joinPoint.getSignature().getName());System.out.println("**********");}@After("com.lei.demo.aop.aspectj.PointcutsDefinition.customerLog()")public void logAfter(JoinPoint joinPoint){System.out.println("logAfter() is running ...");System.out.println("hijacked:"+joinPoint.getSignature().getName());System.out.println("**********");}
}

 

注释:

1.       @Before@After使用PointcutsDefinition中的方法签名代替Pointcut表达式找到相应的切入点,即通过签名找到PointcutsDefinitioncustomerLog签名上的Pointcut表达式,表达式指定切入点为CustomerBo类中的所有方法。所以此例中AdviceLoggingAdvice,为CustomerBo中的所有方法都加入了@Before@After两种类型的两种操作。

2.       对于PointcutsDefinition来说,主要职责是定义Pointcut,可以在其中第一多个切入点,并且可以用便于记忆的方法签名进行定义。

3.       单独定义Pointcut的好处是,一是通过使用有意义的方法名,而不是难读的Pointcut表达式,使代码更加直观;二是Pointcut可以实现共享,被多个Advice直接调用。若有多个Advice调用某个Pointcut,而这个Pointcut的表达式在将来有改变时,只需修改一个地方,维护更加方便。

 

第三步,配置Spring配置文件,配置文件并没有改变

配置Spring-AOP-AspectJ.xml文件,如下:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><aop:aspectj-autoproxy/><bean id="customerBo" class="com.lei.demo.aop.aspectj.CustomerBo"/><bean id="logAspect" class="com.lei.demo.aop.aspectj.LoggingAspect" /></beans>

 

App.java不变,运行测试代码App.java

输出结果:

logBefore() is running ...

hijacked:addCustomer

**********

addCustomer() is running ...

logAfter() is running ...

hijacked:addCustomer

**********

-------------------------------------------

logBefore() is running ...

hijacked:deleteCustomer

**********

deleteCustomer() is running ...

logAfter() is running ...

hijacked:deleteCustomer

**********

 

三、     切入点表达式

Spring3.0.5帮助文档中的切入点表达式如下:

 

Some examples of common pointcut expressions are given below.

 

the execution of any public method:

execution(public * *(..))


the execution of any method with a name beginning with "set":

execution(* set*(..))


the execution of any method defined by the AccountService interface:

execution(* com.xyz.service.AccountService.*(..))


the execution of any method defined in the service package:

execution(* com.xyz.service.*.*(..))


the execution of any method defined in the service package or a sub-package:

execution(* com.xyz.service..*.*(..))


any join point (method execution only in Spring AOP) within the service package:

within(com.xyz.service.*)


any join point (method execution only in Spring AOP) within the service package or a sub-package:

within(com.xyz.service..*)


any join point (method execution only in Spring AOP) where the proxy implements the AccountService interface:

this(com.xyz.service.AccountService)

'this' is more commonly used in a binding form :- see the following section on advice for how to make the proxy object available in the advice body.

 

any join point (method execution only in Spring AOP) where the target object implements the AccountService interface:

target(com.xyz.service.AccountService)

'target' is more commonly used in a binding form :- see the following section on advice for how to make the target object available in the advice body.

 

any join point (method execution only in Spring AOP) which takes a single parameter, and where the argument passed at runtime is Serializable:

args(java.io.Serializable)
'args' is more commonly used in a binding form :- see the following section on advice for how to make the method arguments available in the advice body.

Note that the pointcut given in this example is different to execution(* *(java.io.Serializable)): the args version matches if the argument passed at runtime is Serializable, the execution version matches if the method signature declares a single parameter of type Serializable.

 

any join point (method execution only in Spring AOP) where the target object has an @Transactional annotation:

@target(org.springframework.transaction.annotation.Transactional)

'@target' can also be used in a binding form :- see the following section on advice for how to make the annotation object available in the advice body.

 

any join point (method execution only in Spring AOP) where the declared type of the target object has an @Transactional annotation:

@within(org.springframework.transaction.annotation.Transactional)

'@within' can also be used in a binding form :- see the following section on advice for how to make the annotation object available in the advice body.

 

any join point (method execution only in Spring AOP) where the executing method has an @Transactional annotation:

@annotation(org.springframework.transaction.annotation.Transactional)

'@annotation' can also be used in a binding form :- see the following section on advice for how to make the annotation object available in the advice body.

 

any join point (method execution only in Spring AOP) which takes a single parameter, and where the runtime type of the argument passed has the@Classified annotation:

@args(com.xyz.security.Classified)

'@args' can also be used in a binding form :- see the following section on advice for how to make the annotation object(s) available in the advice body.

 

any join point (method execution only in Spring AOP) on a Spring bean named 'tradeService':

bean(tradeService)


any join point (method execution only in Spring AOP) on Spring beans having names that match the wildcard expression '*Service':

bean(*Service)

转载于:https://www.cnblogs.com/jcomet/p/5570460.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/470521.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

论文阅读:Spatial Transformer Networks

文章目录1 概述2 模型说明2.1 Localisation Network2.2 Parameterised Sampling Grid3 模型效果参考资料1 概述 CNN的机理使得CNN在处理图像时可以做到transition invariant&#xff0c;却没法做到scaling invariant和rotation invariant。即使是现在火热的transformer搭建的图…

dataframe 排序_疯狂Spark之DataFrame创建方式详解一(九)

创建DataFrame的几种方式1、读取json格式的文件创建DataFrame注意&#xff1a;1. json文件中的json数据不能嵌套json格式数据。2. DataFrame是一个一个Row类型的RDD&#xff0c;df.rdd()/df.javaRdd()。3. 可以两种方式读取json格式的文件。4. df.show()默认显示前20行数据。5.…

【原】npm 常用命令详解

今年上半年在学习gulp的使用&#xff0c;对npm的掌握是必不可少的&#xff0c;经常到npm官网查询文档让我感到不爽&#xff0c;还不如整理了一些常用的命令到自己博客上&#xff0c;于是根据自己的理解简单翻译过来&#xff0c;终于有点输出&#xff0c;想学习npm这块的朋友不可…

论文阅读 - CRNN

文章目录1 概述2 模型介绍2.1 输入2.2 Feature extraction2.3 Sequence modeling2.4 Transcription2.4.1 训练部分2.4.2 预测部分3 模型效果参考资料1 概述 CRNN(Convolutional Recurrent Neural Network)是2015年华科的白翔老师团队提出的&#xff0c;直至今日&#xff0c;仍…

python easygui_Python里的easygui库

想要用python开发一些简单的图形界面&#xff0c;于是接触了easygui库&#xff0c;由于这是新手教程&#xff0c;我会把它写的尽量简单&#xff0c;希望大家都能看懂。1.msgboxmsgbox( )有一个标题&#xff0c;内容和一个ok键(是可以更改的)。举个例子&#xff1a;import easyg…

Rust小技巧 - 让函数既可接受String或str,也可以返回String或str

文章目录1 场景说明2 解决方案2.1 允许不同的输入参数2.2 允许不同的输出参数2.3 让调用方来做处理参考资料1 场景说明 假设我们有一个函数foo&#xff0c;它既要允许&str也要允许String作为输入参数。或是既要允许&str也要允许String作为输出。&str和String之间的…

JSP九大内置对象(转载)

JSP中一共预先定义了9个这样的对象&#xff0c;分别为&#xff1a;request、response、session、application、out、pagecontext、config、page、exception 1、request对象 request 对象是 javax.servlet.httpServletRequest类型的对象。 该对象代表了客户端的请求信息&#xf…

recv发送失败 缓冲区太小_从 GFS 失败的架构设计来看一致性的重要性

作者简介 陈东明&#xff0c;饿了么北京技术中心架构组负责人&#xff0c;负责饿了么的产品线架构设计以及饿了么基础架 构研发工作。曾任百度架构师&#xff0c;负责百度即时通讯产品的架构设计。具有丰富的大规模系统构 建和基础架构的研发经验&#xff0c;善于复杂业务需求下…

Rust小技巧 - 把异步函数放进vector当中

文章目录1 场景说明2 解决方案2.1 无借用参数2.2 有借用参数参考资料1 场景说明 有些时候&#xff0c;我们希望将将异步函数放到vector当中&#xff0c;或者说是注册进vector当中&#xff0c;然后在某个地方遍历这个vector来实现对已经注册了的异步函数的调用。 Cargo.toml中…

好用的记事本_分类记事本软件哪个好用?大家推荐一个苹果手机用的分类记事本便签呗...

随着“互联网”的发展&#xff0c;现在都开始在软件上记事备忘了。那么&#xff0c;都有哪些好用的记事本软件可以选择使用呢&#xff1f;大家在选择记事本软件的时候&#xff0c;都有哪些标准呢&#xff1f;不知道大家的标准是什么&#xff0c;小编有一个不能妥协的标准&#…

tch-rs指南 - Tensor的基本操作

文章目录1 概述2 Tensor的基本操作2.1 Tensor的初始化&#xff08;1&#xff09;通过数组创建&#xff08;2&#xff09;通过默认方法创建&#xff08;3&#xff09;通过其他的tensor创建&#xff08;4&#xff09;通过opencv::core::Mat创建2.2 Tensor的属性2.3 Tensor的运算&…

命令行运行jmeter脚本

1、通过gui界面的jmeter创建一份脚本&#xff1b;2、打开cmd,切换到jmeter程序的Bin目录&#xff1b;3、执行jmeter.bat -n -t bookair_0613.jmx -l log_3.jtl&#xff1b;4、使用gui界面添加一个监听器&#xff0c;打开log_3.jtl文件&#xff0c;来分析测试结果。转载于:https…

bootstrap table 分页_Java入门007~springboot+freemarker+bootstrap快速实现分页功能

本节是建立在上节的基础上&#xff0c;上一节给大家讲了管理后台表格如何展示数据&#xff0c;但是当我们的数据比较多的时候我们就需要做分页处理了。这一节给大家讲解如何实现表格数据的分页显示。准备工作1&#xff0c;项目要引入freemarker和bootstrap&#xff0c;如果不知…

Rust小技巧 - 通过FFI编程运行tensorrt模型

文章目录1 概述2 使用说明2.1 配置说明2.2 修改c头文件2.3 编写build.rs2.4 测试参考资料1 概述 shouxieai/tensorRT_Pro是一个文档完善&#xff0c;效果也很不错的tensorrt库&#xff0c;里面有对yolov5&#xff0c;yolox&#xff0c;unet&#xff0c;bert&#xff0c;retina…

1+X web中级 Laravel学习笔记——查询构造器简介及新增、更新、删除、查询数据

一、新增数据 插入多条数据&#xff1a; 二、更新数据 更新某条数据&#xff1a; 自增某字段的值&#xff1a; 自减某字段的值&#xff1a; 自增的同时改变其他字段的值&#xff1a; 三、删除数据 四、查询 查面构造器查面数据 有以下几种方法 get&#xff08;&…

【HTML5】Canvas画布

什么是 Canvas&#xff1f; HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像。 画布是一个矩形区域&#xff0c;您可以控制其每一像素。 canvas 拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。 * 添加 canvas 元素。规定元素的 id、宽度和高度&#xff1a; &l…