1

I am looking for a pointcut expression that might satisfy my needs, I have already figured one out but seems to be having some performance issues, and I believe there should be an easier solution.

I have a lot of packages like

  • aaa.bbb.v3.groups.GroupController,
  • aaa.bbb.v3.groups.GroupService,
  • aaa.bbb.v3.products.ProductController,
  • aaa.bbb.v3.products.ProductService.

What I wish to cover is all @RestController calls inside my v3 package. I guess it should be something like this but cant figure it out for now:

execution(* aaa.bbb.v3.*.* Controller( * ))

My solution for now was

@Pointcut(
  "within(@org.springframework.web.bind.annotation.RestController *) && " + 
  "execution(* aaa.bbb.v3.*..*(..))"
)

and it was working fine, but seems it has some performance issues as its analyzing all the code, and it should only be for controllers.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
COCOLICHE
  • 35
  • 4
  • I improved code formatting and also formatting in general as well as some minor spelling issues. Please give your questions some more love in the future. Make it easy for the community to read them to increas the probability of getting help. Thank you. – kriegaex Apr 19 '22 at 05:12
  • Can you please explain why you think you have performance issues and how you know which part of the code is being analyzed? It would be easier to help you then, otherwise everyone has to try for themselves and guess about how to reproduce your situation. – kriegaex Apr 19 '22 at 05:14
  • Feedback, please. Two people have commented on and answered your question. Yet there was no reaction from you, which is not particularly polite. – kriegaex Apr 23 '22 at 01:04

2 Answers2

1

bean PCD could be used here and this answer assumes that the Spring controller beans are in a consistent fashion as given in the example .

@Component
@Aspect
public class ControllerExcecutionAspect {

    @Pointcut("within(aaa.bbb.v3..*) && bean(*Controller))")
    public void v3Controller() {
    }
    
    @Before("v3Controller()")
    public void adviceBefore(JoinPoint jp) {
        System.out.println(jp.getSignature().toShortString());
    }
    
}

within : Any join point (method execution only in Spring AOP) within the v3 package or one of its sub-packages:

bean : Any join point (method execution only in Spring AOP) on Spring beans having names that match the wildcard expression *Controller:

Reference : https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-pointcuts-examples

Do go through Writing Good Pointcuts section as well.

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

I am not sure if it is going to improve anything, but you could try (untested):

@Pointcut(
  "within(aaa.bbb.v3..*) && " + 
  "@within(org.springframework.web.bind.annotation.RestController)"
)

BTW, not seeing all your code, I have no way of knowing if you combine pointcuts and other parts of your total pointcut might use other, more expensive constructs, such as this(), target(), @target().

kriegaex
  • 63,017
  • 15
  • 111
  • 202