title: Spring: detect lazy beans at runtime
body: <p>Is there any way to detect wether a bean is lazily initialized that works reliably and for <em>every</em> bean in context?</p>
  
  <p>In particular a bean such as this one:</p>
  
  <pre><code>@Configuration
  class MyConfig() {
  
  @Bean
  @Lazy
  Foo foo() {
    return new Foo();
  }
  </code></pre>
  
  <p>I could not find any way to programmatically detect 'foo' is lazy.</p>
  
  <p>Other kind of lazy beans such as this:</p>
  
  <pre><code>@Lazy
  @Component
  class Bar {
    ...
  }
  </code></pre>
  
  <p>can be detected with something like:</p>
  
  <pre><code>boolean isLazy = applicationContext.findAnnotationOnBean(beanName, Lazy.class) != null
  </code></pre>
  
  <p>this is not reliable, it can produce false-positives, for example if a <code>Bar</code> bean is part of a configuration that initializes it eagerly.</p>