title: Spring: Configuration
body: <p>In Spring Java configuration, suppose I want to re-use a <code>@Bean</code> in another <code>@Bean</code> definition. I can do this either in one file:</p>
  
  <pre><code>@Bean
  public A buildA() {
      return new A();
  }
  
  @Bean
  public B buildB() {
      return new B(buildA());
  }
  </code></pre>
  
  <p>or I can configure A in one file and autowire it in another file like (field injection for brevity):</p>
  
  <pre><code>@Autowired
  private A a;
  
  @Bean
  public B buildB() {
      return new B(a);
  }
  </code></pre>
  
  <p>I wonder, if the two possibilities are exactly the same? For me it looks as if, the first version might instatiate A twice, while the second doesn't.</p>
  
  <p>I am asking this, since in my special use case, A is establishing a connection to a messaging broker and I have several Bs that consume the stream (I use <code>.toReactivePublisher()</code> from spring integration in A), and I don't want to connect twice or more to the broker.</p>