title: How to instrument / advice a Spring Data (JPA) repository?
body: <p>I'm failing in my effort to advice a spring data jpa repository. The goal is to instrument (around) all non-<code>void</code> public methods in a particular repository annotated with a custom annotation (ResourceNotFound in this example) and throw an exception when the return value is either <code>null</code> or an empty collection.</p>
  
  <pre><code>@Repository 
  @ResourceNotFound
  @Transactional(readOnly = true)
  public interface CityRepository extends JpaRepository&lt;City, Long&gt;, JpaSpecificationExecutor&lt;City&gt; { … }
  </code></pre>
  
  <p>The following advice is to wire all public methods of the implementations of the interface annotated with <code>@ResourceNotFound</code>.</p>
  
  <pre><code>@Pointcut("within(com.digitalmisfits.spring.aop.annotation.ResourceNotFound *)")
  public void beanAnnotatedWithResourceNotFound() {}
  
  @Pointcut("execution(public * *(..))")
  public void publicMethod() {}
  
  @Around("beanAnnotatedWithResourceNotFound() &amp;&amp; publicMethod()")
  public Object publicMethodInsideAClassMarkedWithResourceNotFound(ProceedingJoinPoint pjp) throws Throwable {
  
      System.out.println("publicMethodInsideAClassMarkedWithResourceNotFound " + pjp.getTarget().toString());;
  
      Object retVal =  pjp.proceed();
  
      if(((MethodSignature) pjp.getSignature()).getReturnType() != Void.TYPE &amp;&amp; isObjectEmpty(retVal))
          throw new RuntimeException("isObjectEmpty == true");
  
      return retVal;
  }
  </code></pre>
  
  <p>The <code>publicMethodInsideAClassMarkedWithResourceNotFound(…)</code> method works when the pointcut isspecified as:</p>
  
  <pre><code>@Pointcut("execution(public * package.CityRepository+.*(..))")
  </code></pre>
  
  <p>However, the <code>@ResourceNotFound</code> annotation is not being picked up. This might be due to the fact that the underlying class of the repository interface is a (proxied) <code>SimpleJpaRepository</code> which does not have that particular annotation.</p>
  
  <p>Is there a way to propagate @ResourceNotFound to the implementation?</p>
  
  <p>-- update --</p>
  
  <p>Changed the question to reflect the fact that the advice (around) only should apply to repositories with a custom annotation.</p>