Search code examples
javaaopaspectjspring-aoppointcut

Avoid overlaping pointcuts and aspects in AOP


In package com.repository I have :

  1. Standalone interfaces extending spring data Repository
  2. Interfaces extending spring data Repository with my own implementation in the same package
  3. Repository classes implementing my repository interfaces

I would like to measure execution time of all methods from com.repository package (communication with database). But I would like to avoid duplications

now with

@Pointcut("execution(public * com.repository..(..))")

I have some methods logged twice - from interface and from class implementing this interface. I would like either not to log methods from interfaces which have implementing class in the same package, or not to log methods from classes which implement interface from the same package.

How can I express it with pointcut and advice?

My question is a bit related to AspectJ : Issue when combining multiple pointcuts in @Around advice, but it doesn't solve my problem.


Solution

  • If the objects you're monitoring are located to a specific package, why you don't bound the pointcut only to that package.

    So use

    @Pointcut("execution(* com.repository.*.*(..))")
    

    instead of

    @Pointcut("execution(public * com.repository..*.*(..))")
    

    which include "com.repository" package and all its sub packages.