title: Sort spring @Beans by method level @Order annotation
body: <p>My library has to deal with multiple beans (interceptors) that are specified in arbitrary order (because they are spread over multiple configuration files).
  Before I can apply them I have to sort them by their priority. I use <code>AnnotationAwareOrderComparator.sort(beans)</code> for that. This works well as long as the <code>@Order</code> annotation is added on the class level of that interceptor.
  But it does not work when I try to use it in @Configuration classes on @Bean methods:</p>
  
  <pre><code>@Configuration
  public class Config {
  
      @Bean
      @Order(1)
      public ServerInterceptor exceptionTranslatingServerInterceptor() {
          return ...;
      }
  
      @Bean
      @Order(2)
      public ServerInterceptor authenticatingServerInterceptor() {
          return ...;
      }
  
      @Bean
      @Order(3)
      public ServerInterceptor authorizationCheckingServerInterceptor() {
          return ...
      }
  
  }
  </code></pre>
  
  <p>But if I add a test like this:</p>
  
  <pre><code>@Test
  void testOrderingOfTheDefaultInterceptors() {
      List&lt;ServerInterceptor&gt; expected = new ArrayList&lt;&gt;();
      expected.add(applicationContext.getBean(ExceptionTranslatingServerInterceptor.class));
      expected.add(applicationContext.getBean(AuthenticatingServerInterceptor.class));
      expected.add(applicationContext.getBean(AuthorizationCheckingServerInterceptor.class));
  
      List&lt;ServerInterceptor&gt; actual = new ArrayList&lt;&gt;(this.registry.getServerInterceptors());
      assertEquals(expected, actual); // Accidentally passes
      // System.out.println(actual);
  
      Collections.shuffle(actual);
      AnnotationAwareOrderComparator.sort(actual);
      assertEquals(expected, actual); // Fails
      // System.out.println(actual);
  }
  </code></pre>
  
  <p>Then the test will fail.
  From my debugging I know that <code>AnnotationAwareOrderComparator.findOrder(Object)</code> always returns null (unspecified) for the order of those beans. Probably because the bean instances aren't proxied and thus neither implement order nor have the order annotation on their class level. Is there a BeanPostProcessor or an config option that I have to enable?</p>
  
  <p><a href="https://i.stack.imgur.com/Zd89Q.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Zd89Q.png" alt="Controll-Flow"></a></p>
  
  <p>How do I tell spring to either preserve the annotated order or use the application context's bean definitions to sort the beans appropriately?</p>