Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspectJ : Issue when combining multiple pointcuts in @Around advice

I'm a beginner in AspectJ so please guide me to resolve the issue happening as per the below approach.

    @Aspect
    public class TestAop {

    @Pointcut("execution(public * com.packg.foo.ClassOne.*(..))")
    public void fooPoint()

    @Pointcut("execution(public * com.packg.cat.ClassTwo.*(..))")
    public void catPoint()

    @Pointcut("execution(public * com.packg.roo.ClassThree.*(..))")
    public void rooPoint()

    @Around("fooPoint() || catPoint() || rooPoint()") 
    public Object myAdvice(ProceedingJoinPoint joinPoint) {
    //do something like joint proceed and all
    }

When it is not working ? If I combine all the three pointcuts with OR .

When it is working ? If i keep only two pointcuts it is working.

Am I vioalating any rules of @around advice. Is it possible to have multiple execution/pointcuts?

Hoping for the answers...

like image 499
mdparthi Avatar asked Jul 18 '12 13:07

mdparthi


People also ask

What is advice in AspectJ?

AspectJ supports three kinds of advice. The kind of advice determines how it interacts with the join points it is defined over. Thus AspectJ divides advice into that which runs before its join points, that which runs after its join points, and that which runs in place of (or "around") its join points.

Which of the following is true about after advice in Spring AOP?

After throwing advice runs when a matched method execution exits by throwing an exception. It is declared using the @AfterThrowing annotation: import org. aspectj.

Which of these advices when used will have a chance to execute some code after the join point execution throws an exception?

After (finally) Advice: An advice that gets executed after the join point method finishes executing, whether normally or by throwing an exception. We can create after advice using @After annotation.


2 Answers

I had same problem but better solution IMO is(works for me):

@Aspect
public class TestAop {

@Pointcut("execution(public * com.packg.foo.ClassOne.*(..)) || execution(public * com.packg.cat.ClassTwo.*(..)) || execution(public * com.packg.roo.ClassThree.*(..))")
public void fooPoint(){}

@Around("fooPoint()")
public Object myAdvice(ProceedingJoinPoint joinPoint) {
//do something like joint proceed and all
}
like image 143
adamr Avatar answered Feb 08 '23 23:02

adamr


I have solved the above issue by creating different advice for each pointcut. I just found an alternate solution, but i'm still not convinced with that.

@Aspect
public class TestAop {

@Pointcut("execution(public * com.packg.foo.ClassOne.*(..))")
public void fooPoint()

@Pointcut("execution(public * com.packg.cat.ClassTwo.*(..))")
public void catPoint()

@Pointcut("execution(public * com.packg.roo.ClassThree.*(..))")
public void rooPoint()

@Around("fooPoint()") 
public Object myFooAdvice(ProceedingJoinPoint joinPoint) {
//do something like joint proceed and all
}

@Around("catPoint()") 
public Object myCatAdvice(ProceedingJoinPoint joinPoint) {
//do something like joint proceed and all
}

@Around("rooPoint()") 
public Object myRooAdvice(ProceedingJoinPoint joinPoint) {
//do something like joint proceed and all
}
like image 38
mdparthi Avatar answered Feb 08 '23 23:02

mdparthi