AspectJ学习笔记之Pointcut

AspectJ介绍(一) AspectJAOP概念AspectJ发现问题解决问题AspectJ的设计理念(概念)Advice(通知)Joint point(连接点)Pointcut(切入点)集成AspectJ插件集成AspectJgradle集成来个简单demo爽一下代码运行效果总结 AOP概念 AOP是Aspect Oriented Programming的缩写,即『面向切面编程』。它和我们平时接触到的OOP都是编程的不... 阅读详情

该文是我读AspectJ in Action时的一些学习笔记和自己的一些学习总结,当时是用英文记下的(因为书是英文的嘛,嘿嘿,而且个人认为看英文文档更能理解的透彻一些,毕竟是作者的原话嘛),现在放到网上和大家共享,并翻译成中文,如果哪处我翻译的不够恰当,理解的不够准确,请各位看英文部分以获得更准确的信息并给予我指正。(ah011@163.com)

 

Pointcuts

public  pointcut  accountOperations :call(*Account.*(..))

这是定义了一个pointcut,其中:

public 是access specifer

pointcut 是keyword

accountOperations 是poincut name

call是pointcut type;;pointcut能与其他pointcut通过或(||)、与(&&)以及非(!)操作符联合

Account是signature

 

l         常用pointcut类型

1)      call

2)      execution

3)      target

4)      args

5)      within

6)      cflow

 

l         基本概念

如果对上面的概念不是特别了解没有关系,下面就介绍一些基本的概念:

Joint Point

A join point is an identifiable point in the execution of a program. It could be a call to a method or an assignment to a member of an object.

join point 是程序执行过程中可以被识别的点。它可以是对一个函数的调用或是对象的一个属性。(注:spring的AOP只能做到对函数调用的拦截)

例子:

Public class Account

{

 void credit(float amount)

{

       _balance += amount;

}

}

这个例子中的join point包括Account类中执行credit()方法和对_balance的操作。

 

Pointcut

A pointcut is a program construct that selects join points and collects context at those points. For example, a pointcut can select a join point that is a call to a method, and it could also capture the method’s context, such as the target object on which the method was called and the method’s arguments.

We can write a pointcut that will capture the execution of the credit() method in the Account class shown earlier:

execution(void Account.credit(float))

To understand the difference between a join point and pointcut, think of pointcuts as specifying the weaving rules and join points as situations satisfying those rules.

 

pointcut 是一种程序结构,它用于选取join point并收集这些point的上下文信息。举例来说,pointcut可以是一个调用方法的join point,并且它能捕获这个方法的上下文信息,例如调用这个方法的目标对象和该方法的属性。

我们可以写一个pointcut,它将可以捕获前面Account类中credit()方法的执行:

execution(void Account.credit(float))

 

要想理解join point和pointcut的不同,可以把pointcut想成明确了织入的规则,而join point则声明了符合这些规则的情况(即什么情况下运用织入的规则)。

 

l          笔记要点

1.        In AspectJ, pointcut can be either anonymous or named.

aspectj中,poincut可以是匿名的,也可以是命名的。

 

2.        Notice that the name of the pointcut is at the left of the colon and pointcut definition is at the right. The pointcut definition is syntax that identifies the join points where you want to insert some action.

注意,pointcut的名字应位于冒号的左侧,而pointcut的定义在冒号的右侧。point cut的定义明确了你想在哪个join point插入一些动作。

 

3.        Three wildcard notations are available in AspectJ:

1)      * denotes any number of characters expect the period.

2)      .. denotes any number of characters including any number of period.

3)      + denotes any subclass or subinerface of a given type.

Example:

*Account       Types with a name ending with Account such as SavingsAccout and CheckingAccount

java.*.Data    Types Data in any direct subpackages of the java package, such as java.util.Data and java.sql.Data.

java..*           Any type inside the java package or all of its direct subpackage, such as java.awt and java.util, as well as indirect subpackages such as java.awt.event and java.util.logging

 

以下是AspectJ中的三个通配符:

1)      * 表示匹配任意数量的字符,除了句号(.)

2)      .. 表示比配包含句号在内的任意数量的字符。

3)      + 表示比配任意给定的类型的子类和子接口。

例子:

*Account       匹配以Account结尾的类型名,例如SavingsAccout和

CheckingAccount

java.*.Data  匹配java包下的直接子包下的Data类型,例如java.util.Data和java.sql.Data(注:但不能是java.a.b.Data, 因为*不能包含句号嘛)

java..*           匹配java包下任意类型或者其所有直接子包,例如java.awt和java.util,也可匹配其非直接子包,例如java.awt.event和java.util.logging

 

4.        Please note that in method signatures, the wildcard .. used to denote any type and number of arguments taken by a method.

Example:

*java.io.Reader.read(char[],.. )

Any read() method in the Reader class irrespective of type and number of arguments to the method as long as the first argument type is char[]. In this case, it will mach read(char[])  and  read(char[], int, int), but not read().

请注意在method signatures中,通配符..被用于表示匹配包含任意类型和任意数量的参数的方法。

例子:

*java.io.Reader.read(char[],.. )

Reader类中的任何read()方法,无论参数是什么类型,数量如何,只要其首参数是char[]类型,就可以匹配。这种情况下,它可以匹配read(char[]),也可以匹配read(char[], int, int),但不能匹配read()。

 

5.        The execution join point is on the method body itself.

执行类型的join point是方法体本身。

Example:

In this code snippet, the point for the execution of the debit() method is the whole method body. This means that we can write advice for this join point to the applied before, after, and around the body.

在这个程序小片段里,debit()方法的执行类型joint point是整个方法体。这意味着我们可以为它写符合要求的advice(通知),包括before、after、around。(注:关于advice,将在下一篇中介绍)

6.        The method call join point occurs at the place where this method is being invoked.

方法调用的join point放生在方法被引用的地方。

Example:

Account account = …...;

account.debit(100);  //debit() method call join point

 

Note that the code that forms the arguments is not a part of the join point. For example, if the debit() method is called in the statement account.debit(Currend.round(100.2345)), the call Current.round(100.2345) is not part of the debit() method call join point.

注意,代码形式的参数不是call join point的一部分。例如如果debit()方法以如下形式调用,account.debit(Currend.round(100.2345)),那么Current.round(100.2345)不是其method call join point的一部分。

 

7.        execution 和call join point 的区别

我的理解是:

1)  在你想针对某个方法进行某些advice的织入时,应该用execution,即当想在某个方法的方法体之前,之后,之中织入某些advice时使用。

Example:

在这段代码中,如果想在debit()的方法体,即

之前、之后、之中织入一些advice时使用。

2)  而call是想在调用某个方法的地方之前、之后织入某些advice时使用的。

Example:

Account account = …...;

account.debit(100);  //即在句前、后加advice时使用。

 

8.        Execution Object Pointcuts (this和target)

1)      These pointcuts match the join points based on the types of the objects at execution time.

这种pointcut用于在运行期匹配对象的类型。

2)      In addition to matching the join points, these pointcuts are used to collect the context at the specified join point.

另外,这种pointcut可以用于收集join point附近的上下文信息。

3)      The this() pointcut takes the form this(Type or ObjectIdentifier); it matches all join points that have a this object associated with them that is of the specified type or the specified ObjectIdentifier’s type. In other words, if you specify Type, it will match the join points where the expression this instanceof <Type> is true. The form of this pointcut that specifies ObjectIdentifier is used to collect the this object. If you need to match without collecting context, you will use the form that uses Type, but if you need to collect the context, you will use the form that uses ObjectIdentifier.

this() 型pointcut的基本形式为this(Type or ObjectIdentifier);它匹配所有有this对象的pointcut,而这些pointcut就是已声明的类型或对象标识符的类型。换句话说就是,如果你声明了Type,当表达式this instanceof <Type>为真时将匹配。声明为ObjectIdentifier的pointcut将用于收集this对象的上下文信息。如果你想只匹配而不收集上下文,可以使用Type,反之则使用ObjectIdentifier

4)      The target() pointcut is similar to the this () pointcut. The target() pointcut is normally used with a method call join point.

target() pointcut 和this() pointcut类似。target() pointcut 一般和call join point 搭配使用。

5)      Example:

this(Account)        All join points where this is instanceof Account. This will match all join points like methods calls and field assignments where the current execution object is Account, or its subclass, for example, SavingsAccount.

target(Account)     All the join points where the object on which the method called is instanceof Account. This will match all join points where the target object is Account, or its subclass, for example, SavingsAccount.

6)      target 匹配的是哪个对象调用了某方法

Example:

ResgisterBuiness rb = new ResgisterBuiness();

forwardName = rb.doRegister(mapping.form);

对于doRegister方法来说,rb就是target

 

9.        Lexical-structure based pointcuts

1)      A lexical scope is a segment of source code. The within() pointcuts take the form of within(TypePattern) and are used to capture all the join points within the body of the specified classes and aspects, as well as any nested classes. The withincode() pointcuts take the form of either withincode(MethodSignature) or withincode(ConstrutorSignature) and are used to capture all the join points inside a lexical structure of constructor or a method, including any local classes in them.

lexical scope就是一个代码段。within() pointcut 形式是within(TypePattern) ,用来捕获声明的类、方面、还有内部类体的所有join point。withincode() pointcut的形式是withincode(MethodSignature)和withincode(ConstrutorSignature),用于捕获构造函数或方法的词法结构内的所有join point,包括它们内部的任何local classes。

2)      One common usage of the within() pointcut is to exclude the join points in the aspect itself. For example, the following pointcut excludes the join point corresponding to the calls to all print methods in the java.io.PrintStream class that occur inside the TraceAspect itself:

call(*java.io.PrintStream.print*(..)) && ! within(TraceAspect)

 

3)      this 和within的作用差不多,以下为不同点:

<1> within包含内部类,但不包含子类

<2> this包含子类,但不包含内部类

 

10.    Argument pointcuts

These pointcuts capture join points based on the argument type of a join point. Similar to execution object pointcuts these pointcuts can be used to capture the context.

Example:

args(String, .. , int)        All the join points in all methods where the first argument is of type String and last argument is of type int.

匹配所有以String为第一个参数类型,以int为最后一个参数类型的方法。

 

11.    Control-flow based pointcuts

1)      The control flow of a join point defines the flow of the program instructions that occur as a result of the invocation of the join point.

join point的控制流定义了程序指令的流程,以join point调用的结果的形式出现。

2)      For example, the Account.debit() method calls Account.getBalance() as a part of its execution; the call and the excution of Account.getBalance() is said to have occurred in the Account.debit() method’s control flow, and therefore it has occurred in the control flow of the join point for the method.

例如:Account.debit()方法调用Account.getBalance()作为它执行的一部分;对Account.getBalance()进行call和execution操作都可以认为是发生在Account.debit()方法的控制流内,所以就是发生在join point的控制流内。

3)      以下是我经过实验后的一些理解:

接着上面的例子,如果Account.getBalance()中又调用了另一个方法,例如abc(),那么abc()的join point也属于Account.debit()的control-flow,也即不管经过了多少层的调用关系,用control-flow pointcut都可以判断出某个join point是否属于一个control-flow;这一点上和within()又有一定的区别,within只能判别出join point是否在一个代码块中出现过,即只能表示一层关系。

4)      cflow(call(*Account.debit(..)) 任何在debit() control-flow中的join point都将被捕获,包括对debit()自身的call(即cflow()被用在了debit()自己的pointcut时的情况)

cflowbelow(call(*Account.debit(..)) 不包括对debit()自身的call

 
AspectJ 在 Android 中的使用 AspectJ 在 Android 中的使用 在介绍 AspectJ 之前,我们先看看常见的几种编程架构思想。 面向对象编程 Object Oriented Programming 面向过程编程 Procedure Oriented Programming 面向切面编程 Aspect Oriented Programming 面向对象、面向过程、面向切面, 这三种是我们常见的三种编程架构思想,... 阅读详情

相关推荐

Android AspectJ详解

今天关于面试的分享就到这里,还是那句话,有些东西你不仅要懂,而且要能够很好地表达出来,能够让面试官认可你的理解,例如Handler机制,这个是面试必问之题。有些晦涩的点,或许它只活在面试当中,实际工作当中你压根不会用到它,但是你要知道它是什么东西。最后在这里小编分享一份自己收录整理上述技术体系图相关的几十套腾讯、头条、阿里、美团等公司19年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。还有。

2401_84010762的博客 1207

Spring系列之@Aspect中@Pointcut 12种用法

先了解几个概念 文中会涉及几个概念,先了解一下。 target 用来表示目标对象,即需要通过aop来增强的对象。 proxy 代理对象,target通过aop增强之后生成的代理对象。 AspectJ AspectJ是什么? AspectJ是一个面向切面的框架,是目前最好用,最方便的AOP框架,和spring中的aop可以集成在一起使用,通过Aspectj提供的一些功能实现aop代理变得非常方便。 AspectJ使用步骤 1.创建一个类,使用@Aspect标注 2.@Aspect标注的类

azhou的代码园 2401

ComfyUI+minimaxh3本地部署:漫剧分镜批量生成工作流全实操

视频生成模型正从云端API向本地化部署迁移,背后的技术逻辑是将扩散模型的文本编码、潜空间采样、VAE解码等阶段拆分为可编排的独立环节。ComfyUI作为节点化管线工具,恰好能以可视化连线方式管理这些阶段,让生成过程可控、可复用、可批量执行。minimaxh3模型支持本地推理,配合LoRA微调与参数优化,能够在保证角色一致性的前提下,从分镜脚本批量产出连续剧情片段,规避云端排队、成本高、素材不可控等工程问题。这套工作流广泛应用于漫剧、短剧和动态条漫的工业化生产,尤其适合需要高频迭代分镜草稿的内容团队。文本以零

weixin_29057065的博客 383

@AspectJ 语法概要

引言 这是继上篇《AspectJ传统语法概要》之后,对@AspectJ语法的一个整理。 传统的AspectJ固然强大,但需要特定的编译器ajc支持。为了避免这样的依赖,以“Keeping the code plain Java”的方式去实现AOP,AspectJ提供了一种略微区别于传统语法的形式:@AspectJ。@AspectJ使用了Annotation作为实现aspect各要素的基础,使实...

weixin_30631587的博客 349

超详细YOLOv8实例分割全程概述:环境、训练、验证与预测详解

这篇博客提供了一个全面的指南,介绍了如何使用 Ultralytics YOLOv8-seg 系列模型进行对象检测和分割任务。内容从环境搭建、模型选择、训练、验证到预测的完整流程都有详细说明。为想要使用 YOLOv8-seg 系列模型的开发者提供了一站式的解决方案。它不仅涵盖了从环境配置到模型部署的详细步骤,还解释了如何解读和使用模型输出,使得用户能够根据自己的需求选择合适的模型,并能够有效地训练和评估它们。

CDBmax的博客 6万+

Obsidian与GPT协作:数学公式从KaTeX到MathJax的无缝转换实践

本文针对Obsidian用户从GPT等AI助手复制数学公式时遇到的KaTeX与MathJax格式不兼容问题,提供了高效的解决方案。重点介绍了KaTeX to MathJax插件的核心作用,它能自动将AI生成的KaTeX格式公式(如\(...\))无缝转换为Obsidian默认支持的MathJax格式($...$),实现复制粘贴后公式即时渲染,极大提升了数学笔记与AI协作的效率和流畅度。

weixin_29209825的博客 146

Spring Boot分布式系统实践【基础模块构建3.3】注解轻松实现操作日志记录

日志注解 前言 spring切面的编程,spring中事物处理、日志记录常常与pointcut相结合 * * Pointcut 是指那些方法需要被执行"AOP",是由"Pointcut Expression"来描述的. Pointcut可以有下列方式来定义或者通过&& || 和!的方式进行组合. * * Spring AOP支持的AspectJ切入点指示符如下: ...

dbqg57671的博客 259

多核异构处理器核间通信:从共享内存到RPMsg协议的实战解析

在多核异构处理器架构中,核间通信是实现高效协同计算的核心技术。其基本原理是通过共享内存作为数据交换的物理媒介,配合硬件信号机制实现同步与通知。从技术价值看,它解决了不同架构核心间的数据共享与指令协同问题,是释放异构计算潜力的关键。在应用场景上,该技术广泛应用于智能物联网设备、汽车电子和工业控制等领域,用于连接高性能应用处理器与实时微控制器。本文以典型的ARM Cortex-A与Cortex-M组合为例,深入剖析了基于共享内存和RPMsg协议的通信机制,涵盖了从硬件基础到软件协议栈的完整实现链路,并提供了针对

weixin_26969269的博客 466

AspectJ AOP的使用(@Before、@PointCut、@Around等)

最近在看一个项目中使用到AOP的功能,现在将自己过去所学的知识梳理一下。AspectJ AOP的使用(@Before、@PointCut、@Around等)

qq_1763467007的博客 6037

55、磁吸附爬壁机器人运动仿真与位置控制

本文针对大型船舶喷涂作业中人工操作存在的高风险与低效率问题,提出一种基于麦克纳姆轮的磁吸附爬壁机器人运动控制方案。通过建立Adams虚拟样机与MATLAB/Simulink控制系统的联合仿真模型,设计了速度和位置双闭环控制系统,采用模糊PI控制器优化速度响应,并引入位置反馈补偿重力引起的垂直偏移。研究结果表明,模糊PI控制器在响应速度、超调抑制和位置稳定性方面优于传统PI控制器,且位置反馈能有效校正横向移动中的下坠现象,尤其在跨越焊缝等复杂工况下仍可保持稳定运行,显著提升了爬壁机器人的运动精度与作业可靠性。

linux6sysadmin的博客 179

结合MATLAB/Simulink和多体动力学仿真软件UM(Universal Mechanism),建立高速列车轴箱轴承的动力学模型,并通过联合仿真分析其正常运行特性和典型故障模式

高速列车是现代交通运输的重要组成部分,其运行速度高、载客量大,对安全性和可靠性提出了极高的要求。轴箱轴承作为高速列车的关键部件之一,承担着传递载荷和支撑车轴的重要作用。然而,由于长期运行在高速、重载和复杂工况下,轴箱轴承容易出现疲劳磨损、润滑失效和裂纹等故障,从而影响列车的安全性。本项目旨在结合MATLAB/Simulink和多体动力学仿真软件UM(Universal Mechanism),建立高速列车轴箱轴承的动力学模型,并通过联合仿真分析其正常运行特性和典型故障模式。

xiaoheshang_123的博客 353

关于AspectJ 中的pointcut 语法

    这两天忙着看AspectJ in Action 为了补一下AOP知识。看了Spring 2.0的规范,其中AOP部分已经基本融合了AspectJ,看来有必要看一看AspectJ了。   看了很多AOP的文章了,AOP这两年发展的很慢,没有什么新意,现在到处都是SOA,SCA了,不过研究了一下,觉得还是很有帮助的。尤其是增加系统的契约性和模块的独立性来说,很有帮助。   当然,学东西,基础很

闲人别居 1727

GEE:关于遥感生态指数(RSEI)的若干疑问

本文记录了若干关于在 Google Earth Engine (GEE)平台上,计算基于主成分分析(PCA)的遥感生态指数(RSEI)的问题。并记录了解决这些问题的一些答案。问题包括,主成分分析的特征值的正负,主成分的占比,去云后空值像素的处理方法,异常值处理方法,如何看载荷等。

养乐多的博客 3432

AspectJ学习笔记之Pointcut(转)

Pointcutspublic pointcut accountOperations :call(*Account.*(..))这是定义了一个pointcut,其中:public 是access speciferpointc...

cuankuangzhong6373的博客 407

【3GPP核心网】【5G】史上最全的5G协议规范总结

物理层信道定义:TS38.211(物理层信号的生成、调制,DMRS和额外DMRS,PRACH configuration index,Point A等介绍)传输信道和控制信道的数据处理:TS38.212(包括复用,信道编码,交织,DCI,DMRS端口号等)物理层控制过程:TS38.213(同步,上行功控,随机接入,UE上报和接收控制消息的过程,SSB等)物理层测量:TS38.215(UE和网络的测量控制,UE测量能力)频率、频点相关F R1:TS38.101-1。

风云说通信 3193

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

AspectJ学习笔记之pointcut

【参考】《AspectJ in Acton 2nd Edition》 【说明】本文仅总结一些概念   1 连接点模型(join point model) 连接点模型包括两个部分:joinpoints:程序的执行点。pointcuts:选择joinpoints的机制。 概念:join point:系统中可标志的程序的执行点,如方法的调用,字段的访问,for循环或者是if 语句都...

299

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

AspectJ形式的Pointcut--spring揭秘学习笔记

@AspectJ形式的Pointcut的声明包含如下两个部分: Pointcut Expression:其载体为@Pointcut,该注解是方法级别的注解,所以Pointcut Expression不能脱离某个方法单独声明,被附着的的方法的名称是Pointcut Designature。Pointcut Expression是真正规定Pointcut匹配规则的地方,可以通过 @Poin...

kristoph的博客 1327

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

AspectJ学习笔记(三)

AspectJ主要有三个地方: JIONPOINT POINTCUT ADVICE下面就从POINTCUT 的指示符和有关的上下文关系,反射开始:thisJoinPoint.getSignature():thisJoi,nPoint是ASPECTJ上下文定义的的一个类:可以访问切口环境,输出的结果是连接点的方法签名,比如(String Test.getName())  pointcut uniqu

猪猪爱睡觉 1129

基于aspectj的pointcut实现

aspectj-pointcut

@siye 471

【深入Spring】Aspectj的pointcut的表达式

一.介绍 标准的Aspectj Aop的pointcut的表达式类型是很丰富的,但是Spring Aop只支持其中的9种,外加Spring Aop自己扩充的一种一共是10种类型的表达式,分别如下。 execution:一般用于指定方法的执行,用的最多。 within:指定某些类型的全部方法执行,也可用来指定一个包。 this:Spring Aop是基于代理...

SmallCatBaby的博客 853

Spring课程 Spring入门篇 7-1 AspectJ介绍及PointCut注解应用

课程链接: 本节主要是理论型: 关键看下节实操。 1    Aspectj是什么 2    如何在Spring中配置@AspectJ 3    组合pointcut的使用 4    切入点支持哪些定义方式 n    aspect java 注解实现要点(不重要) 1   Aspectj是什么 AspectJ是一个面向切面的框架。它可以用Java注解的风格进...

weixin_30399797的博客 189

AspectJ 中的pointcut 语法

<br />来源:http://ljhzzyx.blog.163.com/blog/static/38380312200902952412604/<br /> <br />Spring现在也使用AspectJ的语法: <br />基本的定义如下:<br /><br />public pointcut accountOperations:call(* Account.*(..))<br /><br />1.通配符和pointcut 操作符 <br /><br />* 表示任何数量的字符,除了(.) <br

xyzroundo的专栏 731
上一篇: Eclipse中使用Junit插件测试
下一篇: AspectJ学习笔记之Advice
CenturyMagus
博客等级 码龄23年 15粉丝 41原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值