Quantcast
Channel: Spring AOP: What's the difference between JoinPoint and PointCut? - Stack Overflow
Viewing all articles
Browse latest Browse all 19

Answer by Ahmad for Spring AOP: What's the difference between JoinPoint and PointCut?

$
0
0

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.


Viewing all articles
Browse latest Browse all 19

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>