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

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

$
0
0

Definitions

As per the documentation:

Join point: a point during the execution of a program, such as theexecution of a method or the handling of an exception.

You can consider Joint Points as events in execution of a program. If you are using Spring AOP, this even is limited to invocation of methods. AspectJ provides more flexibility.

But you never handle all events as you don't eat all the food in the menu when you go to a restaurant (I don't know you, you might! But, I certainly don't). So you make a selection of events to handle and what to do with them. Here goes Pointcuts. As per the documentation,

Pointcut: a predicate that matches join points.

Then you associate what to do with the Pointcut, there goes Advice. As per the documentation,

Advice is associated with a pointcut expression and runs at any join point matched by the pointcut.

Code

package com.amanu.example;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;/** * @author Amanuel Nega on 10/25/16. */class ExampleBussinessClass {    public Object doYourBusiness() {        return new Object();    }}@Aspectclass SomeAspect {    @Pointcut("execution(* com.amanu.example.ExampleBussinessClass.doYourBusiness())")    public void somePointCut() {    }//Empty body suffices    @After("somePointCut()")    public void afterSomePointCut() {        //Do what you want to do after the joint point is executed    }    @Before("execution(* *(*))")    public void beforeSomePointCut() {        //Do what you want to do before the joint point is executed    }}

Explanation of Code

  • ExampleBusinessClass when proxy-ed, is our target!
  • doYourBusiness() is a possible joint point
  • SomeAspect is our aspect that crosses in to multiple concerns such ass ExampleBusinessClass
  • somePointCut() is a definition of a point cut that matches our joint point
  • afterSomePointCut() is an advice that will be executed after our somePointCutpoint cut that matches doYourBusiness()joint point
  • beforeSomePointCut() is also an advice that matches all public method executions. Unlike afterSomePointCut, this one uses an inline point cut declaration

You can look at the documentation if you don't believe me. I hope this helps


Viewing all articles
Browse latest Browse all 19

Trending Articles



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