1

anyone have an idea of how doing that :

I have two pointcut, one generic and an one specific. The two pointcuts will intercept the same method invocation but I want to just call the specific pointcut. Someone Have an idea.

Why I want to do that : imagine a spring repository, I want to intercept all find call we a generic pointcut and execute some code to check access. In Some case, I will redefined new find methods with other arguments, access rules. This new queries will be matched automaticaly by the generic pointcut. But I need a different pointcut in that case to match the specific query.

for example I need something like that :

@Pointcut("execution(public * *..*Repository.*(..))")
@Pointcut("execution(public * *..*Repository.find*(..))")

for all methods of my repository I will use the first pointcut but for all find methods I will use the second one. I want that the Overloading will be automatic. I have try @Before("pointcut1 && ! pointcut2") but if I want to make a third pointcut I will need to modify the @Before expression.

Thank

Alex
  • 11
  • 2
  • maybe the negation helps here? so you match `find` and all others that are *not* `find` https://stackoverflow.com/questions/2266850/spring-aop-pointcut-syntax-for-and-or-and-not – vladtkachuk Jul 19 '22 at 13:33
  • Yes the negation work but like I explane I want something more automatics. For each case I will need to negate the specific pointcut to exclude it from the generic. I wan't something more a hierarchical model, execute the most specific first. – Alex Jul 19 '22 at 13:55
  • If having separate aspects is an option , you could order the execution of aspects from more specific to generic . Filter out what is not required on the generic aspect through code or an indicator (threadlocal or something) that the method call is already advised ? Do go through [Advice Ordering](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-ataspectj-advice-ordering) – R.G Jul 19 '22 at 14:51
  • I've thinking about this solution. But i've not thinking to save the state on the local thread – Alex Jul 19 '22 at 15:45
  • Do go through SO [Q&A](https://stackoverflow.com/q/65926489/4214241) . Pure `AspectJ` seems to allow your requirement . With Spring AOP , a thread local variable is what I could come up with – R.G Jul 20 '22 at 00:14
  • @R.G is right in everything he said. Just a minor remark about the sample pointcuts used in this question: `**.` (double asterisk) is not a meaningful part of a type signature. I guess you rather mean `*..` (double dot) instead, e.g. in your case `*..*Repository`. Maybe you want to fix that so as not to confuse other readers. – kriegaex Jul 20 '22 at 15:46

1 Answers1

1

From Spring reference documentation : Advice Ordering

When two pieces of advice defined in different aspects both need to run at the same join point, unless you specify otherwise, the order of execution is undefined.

and

When two pieces of the same type of advice (for example, two @After advice methods) defined in the same @Aspect class both need to run at the same join point, the ordering is undefined

and

advice methods defined in the same @Aspect class that need to run at the same join point are assigned precedence based on their advice type

If having separate aspects is an option , order the execution of aspects from more specific to generic and throw access exception when a specific access check fails. This would prevent execution of subsequent advises down stream. Another option using Spring AOP is to use a threadlocal variable to mark and identify if a method execution is already advised by a more specific advice.

Do go through the following SO Question and Answer which discusses pure AspectJ for greater control on aspect execution.

R.G
  • 6,436
  • 3
  • 19
  • 28