title: Spring - should I use @Bean or @Component?
body: <p>Here is the current code at my work.</p>
  
  <p><strong>Method 1</strong></p>
  
  <pre><code>@Configuration
  public class AppConfig {
  
      @Bean
      @Autowired(required = false)
      public HttpClient createHttpClient() {
          // do some connections configuration
          return new HttpClient();
      }
  
      @Bean
      @Autowired
      public NameClient nameClient(HttpClient httpClient,
                                   @Value("${ServiceUrl:NotConfigured}") 
                                   String serviceUrl) {
          return new NameClient(httpClient, serviceUrl);
      }
  
  }
  </code></pre>
  
  <p>And the <code>NameClient</code> is a simple POJO looks like following</p>
  
  <pre><code>public class NameClient {
      private HttpClient client;
      private String url;
  
      public NameClient(HttpClient client, String url) {
          this.client = client;
          this.url = url;
      }
  
      // other methods 
  }
  </code></pre>
  
  <p>Instead of using <code>@Bean</code> to configure, I wanted to follow this pattern:</p>
  
  <p><strong>Method 2</strong></p>
  
  <pre><code>@Configuration
  public class AppConfig {
  
    @Bean
    @Autowired(required = false)
    public HttpClient createHttpClient() {
      // do some connections configuration
      return new HttpClient();
    }
  }
  </code></pre>
  
  <p>And use auto-scanning feature to get the bean</p>
  
  <pre><code>@Service //@Component will work too
  public class NameClient {
  
      @Autowired
      private HttpClient client;
  
      @Value("${ServiceUrl:NotConfigured}")
      private String url;
  
      public NameClient() {}
  
      // other methods 
  }
  </code></pre>
  
  <p>Why the first method above is used/preferred? What is the advantage of one over the other? I read about the difference between using <code>@Component</code> and <code>@Bean</code> annotations.</p>