title: Java Configuration vs Component Scan Annotations
body: <p>Java configuration allows us to manage bean creation within a configuration file. Annotated <code>@Component</code>, <code>@Service</code> classes used with component scanning does the same. However, I'm concerned about using these two mechanisms at the same time.</p>
  <p>Should Java configuration and annotated component scans be avoided in the same project? I ask because the result is unclear in the following scenario:</p>
  <pre><code>@Configuration
  public class MyConfig {
      @Bean
      public Foo foo() {
          return new Foo(500);
      }
  }
  
  ...
  
  @Component
  public class Foo {
      private int value;
      
      public Foo() {
      }
      public Foo(int value) {
          this.value = value;
      }
  }
  
  ...
  
  public class Consumer {
      @Autowired
      Foo foo;
  
      ...
  }
  </code></pre>
  <p>So, in the above situation, will the Consumer get a Foo instance with a 500 value or 0 value? I've tested locally and it appears that the Java configured Foo (with value 500) is created consistently. However, I'm concerned that my testing isn't thorough enough to be conclusive.</p>
  <p>What is the real answer? Using both Java config <em>and</em> component scanning on @Component beans of the same type seems like a bad thing.</p>