title: Spring @ConditionalOnBean is not finding the the bean
body: <p>I am using spring boot: 2.5.7</p>
  <p>I have a starter in my application that is not working.
  I opened the code in order to understand the reason and notice a weird behavior:</p>
  <p>The starter code is:</p>
  <p>AutoConfiguration:</p>
  <pre><code>@Configuration
  @EnableConfigurationProperties(MyConfigProperties.class)
  public class MyCompanyAutoConfiguration {
  
  
    @Bean
    @ConditionalOnBean(MyConfigProperties.class)
    public MeterRegistry defaultNewRelicMeterRegistry(MyConfigProperties config) {
      System.out.println(&quot;called method&quot;);
      return MyCompanyNewRelicRegistry.builder(config).build();
    }
  
  }
  </code></pre>
  <p>PropertyBean</p>
  <pre><code>@Configuration
  @ConfigurationProperties(prefix = &quot;company.config.newrelic&quot;)
  @ConditionalOnProperty(prefix = &quot;company.config.newrelic&quot;, name = &quot;apiKey&quot;)
  @ConditionalOnClass(NewRelicRegistryConfig.class)
  public class MyConfigProperties implements NewRelicRegistryConfig {
  
     // getters and setters
     ...
  }
  </code></pre>
  <p>Factory file (spring.factories)</p>
  <pre><code>org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.company.MyCompanyAutoConfiguration
  </code></pre>
  <p>When I start my application, MyCompanyNewRelicRegistry bean is not been created.</p>
  <p>I find out that &quot;ConditionalOnBean&quot; is the problem, the code does not print &quot;called method&quot; when use this annotation. It is weird because MyConfigProperties bean is initialized (I saw calling the applicationContext.getBean after container have started).</p>
  <p>When I remove the annotation @ConditionalOnBean, everything works well and the method is called.</p>
  <p>Why ConditionalOnBean is not finding MyConfigProperties that time?</p>
  <p>Note: all properties and classes used in other conditions are ok.</p>
  <p><strong># UPDATE</strong></p>
  <p>I changed to:</p>
  <pre><code>@Configuration
  @EnableConfigurationProperties
  @ComponentScan
  public class MyCompanyAutoConfiguration {
  
  
    @Bean
    @ConditionalOnBean(MyConfigProperties.class)
    public MeterRegistry defaultNewRelicMeterRegistry(MyConfigProperties config) {
      System.out.println(&quot;called method&quot;);
      return MyCompanyNewRelicRegistry.builder(config).build();
    }
  
  }
  </code></pre>
  <p>The above example works.</p>
  <p>I saw in log that spring try to load &quot;defaultNewRelicMeterRegistry&quot; first, when evaluate the conditionalBean, its not created yet.</p>
  <p>When I remove the ConditionalOnBean the spring will load the property in order to inject it.</p>
  <p>May the componentScan &quot;force&quot; spring to load all other beans before the declared bean in autoconfiguration class, make sense?</p>