title: Spring @Aspect & Maven. Not working in external modules, but it's working witouth annotations
body: <p>I've a question about Spring AOP and AspectJ, defining aspects, pointcut etc with annotations.</p>
  
  <p>The context is this:</p>
  
  <ul>
  <li>I have 2 maven modules (A and B). </li>
  <li>Module B uses module A (via <code>&lt;dependency&gt;...&lt;/dependency&gt;</code> in maven) and import his spring context.</li>
  <li>I'm trying to write and use an very stupid test aspect (catch all public operations).</li>
  <li>This aspect is defined in module A, </li>
  <li>I wan't that this aspect works when I call any public operation in A or B. </li>
  </ul>
  
  <p>Well, now the "postergeist". My first aproach was define all in XML. Aspect, pointcut, etc. And it works really well. This is the code:</p>
  
  <p><b>ApplicationContext</b></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;
  &lt;aop:config&gt;
      &lt;aop:aspect id="myAspectAOP" ref="myAspect"&gt;
          &lt;aop:around method="validateCall"
              pointcut="execution(public * *(..))" /&gt;
      &lt;/aop:aspect&gt;
  &lt;/aop:config&gt;
  &lt;context:component-scan base-package="com.mypackage"/&gt;      
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><em>MyAspect.java</em> and <em>Test.java</em> is the same that the next case (without Aspect annotations). I don't put it here yet to avoid spreading much text.</p>
  
  <p>That works fine!</p>
  
  <p>Ok, next aproach. Using annotations. This is the code:</p>
  
  <p><b>ApplicationContext</b></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;
  
      &lt;aop:aspectj-autoproxy /&gt;
      &lt;context:component-scan base-package="com.mypackage"/&gt;      
  &lt;/beans&gt;
  </code></pre>
  
  <p>And <b>MyAspect</b> class</p>
  
  <pre><code>@Aspect
  @Component //I have tried without this annotation, and it doesn't work
  public class MyAspect {
  
      @Pointcut("execution(public * *(..))")
      public void allPublic() {
      }
  
      @Around("allPublic()")
      public Object validateCall(final ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
  
      // Some Code
  
      }
  }
  </code></pre>
  
  <p><b>Test.java</b> (package com.mypackage)</p>
  
  <pre><code>@Component
  public class Test{
      public void foo(){
      //Some code
      }
  }
  </code></pre>
  
  <p>In this case: </p>
  
  <ul>
  <li>when I put <strong>Test.java</strong> into <strong>module B</strong> and call an public operation (remember, class is in B module), it <strong>doesn't work</strong>. </li>
  <li>BUT when I put <strong>Test.java</strong> into <strong>A Module</strong> and I call a public operation (It's in A module), it <strong>works</strong>. It looks like only works with calls to public operations of classes defined into the same module where Aspect is defined (A module), but not in "externals" (B Module). Remember. B module includes/uses A Module and import his context. But it isn't happening when I use "xml declaration" instead of "annotations declaration"</li>
  <li>Important: <strong>MyAspect.java</strong> is in <strong>A Module</strong> allways</li>
  </ul>
  
  <p>Remember, in the first case (defining all in applicationContext, and not using @Aspect @Pointcut,@...) works allways, in B and A module public operations.</p>
  
  <p>In both cases, the classes are into <i>com.mypackage</i>, so are injected by spring.</p>
  
  <p>I'm really lost, It's the first time I get this kind of "postergeist", and I have been looking and looking into Spring documentation an google for days, but I can't find any solution.</p>
  
  <p>I'm using Spring 3.1.2 and Aspectj 1.6.11</p>
  
  <p>Please, any idea?</p>
  
  <p>Thanks!</p>