title: How to load all beans lazily with @ComponentScan in Spring?
body: <p>I am using Java-based config to set up my Spring application context like this:</p>
  
  <pre><code>@Configuration
  @Lazy
  @ComponentScan(basePackageClasses = {MyProject.class, OtherProject.class})
  public class MyAppConfig {
      ...
  }
  </code></pre>
  
  <p>Beans defined explicitly in the config are loaded lazily, like you would expect. However, scanned classes annotated with <code>@Named</code> are always loaded eagerly. How can I solve this?</p>
  
  <p>Any help is appreciated.</p>
  
  <hr>
  
  <p>Note that for classes in the <code>MyProject</code> package, I can work around this by annotating them with <code>@Lazy</code> as well. But the other project does not have a dependency to Spring and I want to keep it like that (hence <code>@Named</code> and not <code>@Component</code>).</p>
  
  <hr>
  
  <p>Note also that this does not seam to be a problem in XML-based config. There, setting <code>default-lazy-init="true"</code> in the <code>&lt;beans&gt;</code> tag seams to do what I want (although I haven't tested that).</p>