title: Spring Java config same Bean reference
body: <p>Looking through question <a href="https://stackoverflow.com/questions/28747743/autowire-a-bean-within-springs-java-configuration">Autowire a bean within Spring's Java configuration</a> I got a question.</p>
  
  <pre><code>@Configuration
  public class Config {
  
     @Bean
     public RandomBean randomBean(){
         return new RandomBean();
     }
  
     @Bean
     public AnotherBean anotherBean(){
         return new AnotherBean(randomBean()); // this line
     }
  
  }
  </code></pre>
  
  <p>How Spring guarantees that method <code>randomBean()</code> will return the same reference as one which was injected into <code>AnotherBean</code>?</p>
  
  <p>Is it achieved via proxies?</p>
  
  <p>On the other hand, doing it with providing dependencies as method parameters is quiet obvious:</p>
  
  <pre><code>@Configuration
  public class Config {
  
     @Bean
     public RandomBean randomBean(){
         return new RandomBean();
     }
  
     @Bean
     public AnotherBean anotherBean(RandomBean randomBean){
         return new AnotherBean(randomBean);
     }
  
  }
  </code></pre>
  
  <p><strong>Edit:</strong> finally, I found this behavior described in <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-java-further-information-java-config" rel="nofollow noreferrer">Further information about how Java-based configuration works internally</a> topic.</p>