title: Spring @Configuration and <context:component-scan />
body: <p>I have a scenario <a href="https://stackoverflow.com/questions/6653495/configure-spring-security-on-embedded-jetty-in-spring">configuring Spring Security on embedded Jetty</a> which seems to be somewhat solved if I make use of JavaConfig to configure the Jetty server.</p>
  
  <p>As a result, it's looking like JavaConfig rather than XML might be the better option for large chunks of the project. However, there are some niceties in the XML namespaces, like <code>&lt;context:component-scan /&gt;</code> which aren't readily available in a <code>@Configuration</code> setting.</p>
  
  <p>I have discovered that <code>ApplicationContextAware</code> is honored for <code>@Configuration</code> classes, so the following is possible</p>
  
  <pre><code>@Configuration
  public class FooConfig implements ApplicationContextAware {
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) {
          ((AnnotationConfigApplicationContext) applicationContext).scan("org.example");
      }
  }
  </code></pre>
  
  <p>The alternative, which is <a href="http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#beans-java" rel="nofollow noreferrer">documented</a>, is to have the <code>@Configuration</code> class use an <code>@ImportResource</code> annotation and pull in an existing XML file:</p>
  
  <pre><code>@Configuration
  @ImportResource("applicationContext-withComponentScan.xml")
  public class BarConfig {}
  </code></pre>
  
  <p>I guess the question is "Is it bad form to abuse <code>ApplicationContextAware</code> in this way, or is it really not abuse"? Something just feels oddly dirty about the approach so I'd not be surprised if the Spring guys had covered this in some way or another that I've not spotted.</p>
  
  <p>For the interested, the problem relates to scanning a Jersey setup with <code>@Resource</code> and <code>@Provider</code> classes that I'd rather not have to manually manage entries in a class/XML configuration.</p>