title: Spring - Multiple Aspects Firing Out of Order
body: <p>I am having trouble getting multiple aspects to fire in a specific order.  I am using the RequestProcessor to do certain things on every incoming request on my controllers, which have a certain parameter</p>
  
  <p>Then I have some specific annotations that I will be adding to only certain methods within my controllers.</p>
  
  <p>FYI I am using Eclipse, Tomcat, Maven and spring with java/annotation based configs. I use Tomcat and the WebApplicationInitializer to load my context, dispatcher, listeners etc.  I have no web.xml.  I can post that or the pom.xml if needed too.</p>
  
  <p>The problem I am getting is that a method that satisfies both the <code>ProcessRequest</code> pointcut and the <code>someAnnotation</code> pointcut is firing the <code>someAnnotation</code> method first even though the order is specified to fire <code>ProcessRequest</code> first.  There is some properties set in the <code>ProcessRequest</code> that is needed in some other annotations.</p>
  
  <p>Here is a simplified version of my code.</p>
  
  <p><strong>Spring Config Class</strong></p>
  
  <pre><code>@Configuration  // Enable Spring Annotation Configuration. Equivalent to &lt;context:annotation-config/&gt;
  @EnableAspectJAutoProxy
  @EnableCaching  // Enable Spring caching
  @EnableWebMvc   // Enable Spring MVC Annotation. Equivalent to &lt;mvc:annotation-driven /&gt;.
  @ComponentScan(basePackages = {"xxx.yyy.zzz"}) // Scan for Spring Components.      Equivalent to &lt;context:component-scan&gt;
  public class WebAppConfig extends WebMvcConfigurerAdapter {
  
      // Other Bean logic here
  
      @Bean
      public RequestProcessor requestProcessor() {
          return new RequestProcessor();
      }
  
      @Bean
      public AnnotationAspect annotationAspect() {
          return new AnnotationAspect();
      }
  }
  </code></pre>
  
  <p><strong>Aspect #1</strong></p>
  
  <pre><code>@Aspect
  @Order(0)
  public class RequestProcessor {
  
      @Pointcut("execution(* xxx.yyy.zzz.api..*.*(xxx.yyy.zzz.objects.api.Request,..)) &amp;&amp; args(request)")
      public void pointcut(Request&lt;?&gt; request) {}
  
      @Before("pointcut(request)")
      public void processRequest(Request&lt;?&gt; request) throws IOException, BadSignatureException {
          // Some logic here that is independent of other and needs to run before other aspect which references annotation
      }
  }
  </code></pre>
  
  <p><strong>Aspect #2</strong></p>
  
  <pre><code>@Aspect
  @Order(1)
  public class AnnotationAspect {
  
      @Before("@annotation(xxx.yyy.zzz.annotation.SomeAnnotation)")
      public void someAnnotation() {
          // Log for this annotation
      }
  
      // Some other annotation methods here
  }
  </code></pre>
  
  <p><strong>Also tried this format <code>implements Ordered</code></strong></p>
  
  <pre><code>@Aspect
  public class RequestProcessor implements Ordered {
  
      @Override
      public int getOrder() {
          return 0;
      }
  
      @Pointcut("execution(* xxx.yyy.zzz.api..*.*(xxx.yyy.zzz.objects.api.Request,..)) &amp;&amp; args(request)")
      public void pointcut(Request&lt;?&gt; request) {}
  
      @Before("pointcut(request)")
      public void processRequest(Request&lt;?&gt; request) throws IOException, BadSignatureException {
          // Some logic here that is independent of other and needs to run before other aspect which references annotation
      }
  }
  </code></pre>
  
  <p>I read over this post and some others but couldn't find anything relevant that worked.</p>
  
  <p><a href="https://stackoverflow.com/questions/9051728/ordering-aspects-with-spring-aop-mvc">Ordering aspects with Spring AOP &amp;&amp; MVC</a></p>
  
  <p>****UPDATE****</p>
  
  <p>So I have been reading through the AspectJ docs on declaring precedence so I thought I would give it a whirl.  I created a simple aspect that only declares precedence and it works just fine.  </p>
  
  <p>Here is my Precedence Aspect:</p>
  
  <pre><code>public aspect AspectPrecedence {
      declare precedence : RequestProcessor, SomeAspect;
  }
  </code></pre>
  
  <p>I am not going to submit this as answer just yet because I would like to understand why the annotation or "implements Ordered" are not functioning properly in my project.</p>
  
  <p>Any insight would be greatly appreciated. Thanks!</p>
  
  <p>****UPDATE 2****</p>
  
  <p>For reference this works locally in my eclipse environment and seemed to work when deployed to AWS via WAR file.</p>
  
  <pre><code>@Aspect
  @DeclarePrecedence("RequestProcessor, SomeAspect")
  public class RequestProcessor {
  
      @Pointcut("execution(* xxx.yyy.zzz.api..*.*(xxx.yyy.zzz.objects.api.Request,..)) &amp;&amp; args(request)")
      public void pointcut(Request&lt;?&gt; request) {}
  
      @Before("pointcut(request)")
      public void processRequest(Request&lt;?&gt; request) throws IOException, BadSignatureException {
          // Some logic here that is independent of other and needs to run before other aspect which references annotation
      }
  }
  </code></pre>