1

In aspectj , can we have multiple pointcuts mapped to the single advice ?

for example below are two separate pointcuts

@Pointcut("execution(* xyz.get(..))")
    void pointcut1() {
    }

@Pointcut("execution(* abc.put(..))")
    void pointcut2() {
    }

so can anyone give idea how to configure these two pointcuts on a single advice ?

because for single pointcut to single advice like below we have

@Before("pointcut1()")
    public void beforeLogging() {
        System.out.println("Before Methods");
    }

how to configure the same advice for multiple pointcuts ?

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Bravo
  • 8,589
  • 14
  • 48
  • 85
  • Because someone just upvoted my answer, I noticed that this old question is still listed as open, even though there are two helpful answers. Please select one to accept and upvote in order to close the question. Thank you. – kriegaex Jun 29 '21 at 09:51

2 Answers2

1

Yes, you can combine pointcuts with logical AND (&&) as well as logical OR(||) operators or negate them by logical NOT (!).

What you probably want here is this:

@Before("pointcut1() || pointcut2()")

The OR makes sense here because in your example a logical AND would always lead to an empty set: a method cannot be in two packages at the same time, but in one of them alternatively.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
0

You can use the || operator. From the docs http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/aop.html (9.2.2 Declaring an aspect):

@Pointcut("execution(public * (..))")
private void anyPublicOperation() {}

@Pointcut("within(com.xyz.someapp.trading..)")
private void inTrading() {}

@Pointcut("anyPublicOperation() || inTrading()")
private void tradingOperation() {}
Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145