Spring AOP:JoinPoint 和 PointCut 有什么区别? [英] Spring AOP: What's the difference between JoinPoint and PointCut?

查看:80
本文介绍了Spring AOP:JoinPoint 和 PointCut 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习面向方面的编程概念和 Spring AOP.我无法理解切入点和连接点之间的区别 - 它们对我来说似乎是一样的.切入点是您应用建议的地方,连接点也是我们应用建议的地方.那有什么区别呢?

I'm learning Aspect Oriented Programming concepts and Spring AOP. I'm failing to understand the difference between a Pointcut and a Joinpoint - both of them seem to be the same for me. A Pointcut is where you apply your advice and a Joinpoint is also a place where we can apply our advice. Then what's the difference?

一个切入点的例子可以是:

An example of a pointcut can be:

@Pointcut("execution(* * getName()")

Joinpoint 的例子有哪些?

What can be an example of a Joinpoint?

推荐答案

Joinpoint:joinpoint 是程序执行中的候选点可以插入一个方面的应用程序.这一点可能是一个方法被调用,一个异常被抛出,甚至是一个被修改的字段.这些是可以将方面的代码插入到应用程序的正常流程中以添加新行为的地方.

Joinpoint: A joinpoint is a candidate point in the Program Execution of the application where an aspect can be plugged in. This point could be a method being called, an exception being thrown, or even a field being modified. These are the points where your aspect’s code can be inserted into the normal flow of your application to add new behavior.

建议:这是一个对象,其中包括对系统范围内的关注点的 API 调用,这些关注点表示要在由点指定的连接点执行的操作.

Advice: This is an object which includes API invocations to the system wide concerns representing the action to perform at a joinpoint specified by a point.

切入点:切入点定义了应在哪些连接点处应用相关联的 Advice.可以在 AOP 框架支持的任何连接点应用建议.当然,您不想在所有可能的连接点上应用所有方面.切入点允许您指定要应用建议的位置.通常,您使用显式类和方法名称或通过定义匹配类和方法名称模式的正则表达式来指定这些切入点.某些 AOP 框架允许您创建动态切入点,根据运行时决策(例如方法参数的值)确定是否应用建议.

Pointcut: A pointcut defines at what joinpoints, the associated Advice should be applied. Advice can be applied at any joinpoint supported by the AOP framework. Of course, you don’t want to apply all of your aspects at all of the possible joinpoints. Pointcuts allow you to specify where you want your advice to be applied. Often you specify these pointcuts using explicit class and method names or through regular expressions that define matching class and method name patterns. Some AOP frameworks allow you to create dynamic pointcuts that determine whether to apply advice based on runtime decisions, such as the value of method parameters.

下图可以帮助你理解Advice、PointCut、Joinpoints.

The following image can help you understand Advice, PointCut, Joinpoints.

来源

使用餐厅类比的解释: 来源@Victor

当您去餐厅时,您会查看菜单并看到多个可供选择的选项.您可以订购菜单上的一项或多项.但是,在您真正订购它们之前,它们只是用餐的机会".一旦您下订单,服务员将其带到您的餐桌上,这就是一顿饭.

When you go out to a restaurant, you look at a menu and see several options to choose from. You can order one or more of any of the items on the menu. But until you actually order them, they are just "opportunities to dine". Once you place the order and the waiter brings it to your table, it's a meal.

Joinpoints 是菜单上的选项,Pointcuts 是您选择的项目.

Joinpoint 是代码中的一个机会,您可以应用一个方面……只是一个机会.一旦您抓住这个机会并选择一个或多个连接点并将一个方面应用于它们,您就拥有了一个切入点.

A Joinpoint is an opportunity within code for you to apply an aspect...just an opportunity. Once you take that opportunity and select one or more Joinpoints and apply an aspect to them, you've got a Pointcut.

来源维基:

Joinpoint 是程序控制流中的一个点,其中控制流可以通过两条不同的路径到达(IMO:这就是为什么调用联合).

A Joinpoint is a point in the control flow of a program where the control flow can arrive via two different paths(IMO : that's why call joint).

建议描述了一类修改其他函数的函数

Advice describes a class of functions which modify other functions

A Pointcut 是 Joinpoint 的匹配 P 样式,即连接点集.

A Pointcut is a matching Pattern of Joinpoint i.e. set of join points.

这篇关于Spring AOP:JoinPoint 和 PointCut 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆