title: Order between @ControllerAdvice and custom Aspect
body: <h1>tl;dr</h1>
  <p>How can I define the order of custom aspects and the <code>@ControllerAdvice</code>?</p>
  <h1>Detailed description</h1>
  <p>I want to decorate various methods (like normal REST-Calls or a <code>@JmsListener</code>) with MDC information on controller-level.</p>
  <p>The idea is: I, as a developer, don't have to think about the MDC data in my controller-methods.<br />
  I want extra MDC-information (e.g. the userId) and I can be sure, that information will be correctly removed.</p>
  <h2>my aspect</h2>
  <pre class="lang-java prettyprint-override"><code>@Aspect
  @Component
  @Order(LOWEST_PRECEDENCE)
  public class MdcSugar {
  
    @Pointcut(&quot;within(@MdcAwareController *)&quot;) // &lt;- MdcAwareController is my annotation
    public void beanAnnotatedWithMdcAwareController() {
      // this pointcut identifies beans annotated with @MdcAwareController
    }
  
    @Pointcut(&quot;execution(public * *(..))&quot;)
    public void publicMethod() {
      // this pointcut identifies public methods
    }
  
    @Pointcut(&quot;publicMethod() &amp;&amp; beanAnnotatedWithMdcAwareController()&quot;)
    public void publicMethodInsideMdcAwareController() {
      // this pointcut identifies public methods on beans annotated with @MdcAwareController
    }
  
    @Before(&quot;publicMethodInsideMdcAwareController()&quot;)
    public void addMdcSugar(JoinPoint joinPoint) {
      log.info(&quot;Entered&quot;);
      MDC.put(&quot;test&quot;, &quot;test&quot;);
    }
  
    @After(&quot;publicMethodInsideMdcAwareController()&quot;)
    public void removeMdcSugar() {
      log.info(&quot;Leaved&quot;);
      MDC.clear();
    }
  }
  </code></pre>
  <h2>example controller</h2>
  <pre class="lang-java prettyprint-override"><code>@RestController
  @MdcAwareController // &lt;- my annotation
  public class SomeController {
  
     // doesn't matter if the method is a JmsListener or something else
     @GetMapping(&quot;/{id}&quot;)
     public String get(@PathVariable String id) {
        MDC.put(&quot;id&quot;, id);
        log.info(&quot;Got request&quot;);
        throw new RuntimeException(&quot;too bad ...&quot;);
     }
  }
  </code></pre>
  <h2>ControllerAdvice</h2>
  <pre class="lang-java prettyprint-override"><code>@ControllerAdvice
  @Order(HIGHEST_PRECEDENCE)
  public class GeneralExceptionHandler extends ResponseEntityExceptionHandler {
  
    // some others cases with different handlers
  
    @ExceptionHandler(RuntimeException.class)
    protected ResponseEntity&lt;HttpErrorResponseBody&gt; handleUncaught(RuntimeException e) {
      log.error(&quot;Uncaught exception occurred&quot;, e);
      return new ResponseEntity&lt;&gt;(e.toString(), new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }
  </code></pre>
  <p>Now I have a problem when a exception occurs. My aspect will always clear the MDC-context <strong>before</strong> the exception-handler jumps in. Log example:</p>
  <blockquote>
  <p>Entered<br />
  [test=test][id=someString] Got request<br />
  [test=test][id=someString] Leaved<br />
  Uncaught exception occurred</p>
  </blockquote>
  <p>But I want something like this:</p>
  <blockquote>
  <p>Entered<br />
  [test=test][id=someString] Got request<br />
  [test=test][id=someString] Uncaught exception occurred<br />
  [test=test][id=someString] Leaved</p>
  </blockquote>
  <p>I already tried to place <code>@Order()</code> on <code>MdcSugar</code> and <code>GeneralExceptionHandler</code>, but with no affect.</p>