Spring AOP: What's the difference between Join

2019-01-29 16:31发布

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()")

What can be an example of a Joinpoint?

13条回答
迷人小祖宗
2楼-- · 2019-01-29 16:51

JoinPoints: These are basically places in the actual business logic where you wish to insert some miscellaneous functionality that is necessary but not being part of the actual business logic. Some examples of JoinPints are: method call, method returning normally, method throwing an exception, instantiating an object, referring an object, etc...

Pointcuts: Pointcuts are something like regular expressions which are used to identify joinpoints. Pontcuts are expressed using "pointcut expression language". Pointcuts are points of execution flow where the cross-cutting concern needs to be applied. There is a difference between Joinpoint and Pointcut; Joinpoints are more general and represents any control flow where we 'may choose to' introduce a cross-cutting concern while pointcuts identifies such joinpoints where 'we want to' introduce a cross-cutting concern.

查看更多
可以哭但决不认输i
3楼-- · 2019-01-29 16:51

JoinPoint: Joinpoint are points in your program execution where flow of execution got changed like Exception catching, Calling other method.

PointCut: PointCut are basically those Joinpoints where you can put your advice(or call aspect).

So basically PointCuts are the subset of JoinPoints.

查看更多
Animai°情兽
4楼-- · 2019-01-29 16:54

Both pertain to the "where" of aspect-oriented programming.

A join point is an individual place where you can execute code with AOP. E.g. "when a method throws an exception".

A pointcut is a collection of join points. E.g. "when a method in class Foo throws an exception".

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-29 16:58

A pointcut is defined on the Aspect - class implementation. The point cut basically refers to the pointcut expression within the advice.

For e.g,

@Before("execution(* app.purchase2.service.impl.*(..))")
public void includeAddOns(RolesAllowed roles) {
..
}

The above means, "includeAddOns" method is called before invoking(due to the @Before advice) any methods(in classes within package "app.purchase2.service.impl")

The whole annotation is called the pointcut @Before("execution(* app.purchase2.service.impl.*(..))")

Joint point is the actual method invocation, which joined the method in package "app.purchase2.service.impl" to the method in aspect class "includeAddOns()".

You can access properties of the join point with the org.aspectj.lang.JoinPoint class.

查看更多
别忘想泡老子
6楼-- · 2019-01-29 16:59

Comparing an AOP language like AspectJ to a data query language like SQL, you can think of joinpoints (i.e. all places in your code where you can weave aspect code) as a database table with many rows. A pointcut is like a SELECT stamement which can pick a user-defined subset of rows/joinpoints. The actual code you weave into those selected places is called advice.

查看更多
▲ chillily
7楼-- · 2019-01-29 16:59

AOP in spring has {Advisor, Advice, Pointcut, Joinpoint}

As you know the main purpose of aop is decoupling the cross-cutting concern logic (Aspect) from the application code, to implement this in Spring we use (Advice/Advisor)

Pointcut is used to filter where we want to apply this advice exactly, like "all methods start with insert" so other methods will be excluded that's why we have in the Pointcut interface {ClassFilter and MethodMatcher}

So Advice is the cross-cutting logic implementation and Advisor is the advice plus the PointCut, if you use only advice spring will map it to advisor and make the pointcut TRUE which means don't block anything. That's why when you use only advice it is applied to all the methods of the target class because you didn't filter them.

But Joinpoint is a location in the program, you can think about it like reflection when you access the Class object and then you can get Method object, then you can invoke any method in this class, and that's how compiler works, if you think like this you can imagine the Joinpoint.

Joinpoint can be with field, constructor or method but in Spring we have joinpoint with methods only, that's why in Spring we have (Before, After, Throws, Around) types of Joinpoint, all of them refers to locations in the class.

As I mentioned you can have advice with no pointcut (no filter) then it will be applied to all the methods or you can have advisor which is [advice + pointcut] which will be applied to specific methods but you can't have advice without joinpoint like pointcut, you have to specify it, and that's why advice types in spring is exactly the same types as the joinpoint so when you choose an advice you implicitly choose which joinpoint.

To wrap up, advice is the implementation logic for your aspect to the target class, this advice should have a joinpoint like before invocation, after invocation, after throwing or around invocation, then you can filter where exactly you want to apply it using pointcut to filter the methods or no pointcut (no filter) so it will be applied to all the methods of the class.

查看更多
登录 后发表回答