5

Looking through question Autowire a bean within Spring's Java configuration I got a question.

@Configuration
public class Config {

   @Bean
   public RandomBean randomBean(){
       return new RandomBean();
   }

   @Bean
   public AnotherBean anotherBean(){
       return new AnotherBean(randomBean()); // this line
   }

}

How Spring guarantees that method randomBean() will return the same reference as one which was injected into AnotherBean?

Is it achieved via proxies?

On the other hand, doing it with providing dependencies as method parameters is quiet obvious:

@Configuration
public class Config {

   @Bean
   public RandomBean randomBean(){
       return new RandomBean();
   }

   @Bean
   public AnotherBean anotherBean(RandomBean randomBean){
       return new AnotherBean(randomBean);
   }

}

Edit: finally, I found this behavior described in Further information about how Java-based configuration works internally topic.

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
  • Can't you just debug? I guess 2 different instances of the RandomBean exist with the code. If you need just one declare a field or `@autowire` the RandomBean and use the autowired instance in the `anotherBean()` method – StanislavL Jun 13 '17 at 09:11
  • 1
    @StanislavL I am not sure that your comment provides me any details for my question – Andrii Abramov Jun 13 '17 at 09:35
  • 1
    spring will create only single instance and that too at the startup so the anotherBean will have same instance of RandomBean. – Sangam Belose Jun 13 '17 at 09:43

2 Answers2

11

There is only one "randomBean" because the default scope is "singleton".(To force Spring to produce a new bean instance each time one is needed, you should declare the bean's scope attribute to be prototype)

singleton

This scopes the bean definition to a single instance per Spring IoC container (default).

prototype

This scopes a single bean definition to have any number of object instances.

Spring guarantees that method randomBean() will return the same reference as one which was injected into AnotherBean By using proxies.

In order to generate proxies, Spring uses a third party library called CGLIB.

  1. Spring enhances classes by generating a CGLIB subclass which interacts with the Spring container to respect bean scoping semantics for methods.

  2. Each such bean method will be overridden in the generated subclass, only delegating to the actual bean method implementation if the container actually requests the construction of a new instance.

  3. Otherwise, a call to such an bean method serves as a reference back to the container, obtaining the corresponding bean by name.

see org.springframework.context.annotation.ConfigurationClassEnhancer.BeanMethodInterceptor

// To handle the case of an inter-bean method reference, we must explicitly check the
// container for already cached instances.

// First, check to see if the requested bean is a FactoryBean. If so, create a subclass
// proxy that intercepts calls to getObject() and returns any cached bean instance.
// This ensures that the semantics of calling a FactoryBean from within @Bean methods
// is the same as that of referring to a FactoryBean within XML. See SPR-6602.
if (factoryContainsBean(BeanFactory.FACTORY_BEAN_PREFIX + beanName) && factoryContainsBean(beanName)) {
    Object factoryBean = this.beanFactory.getBean(BeanFactory.FACTORY_BEAN_PREFIX + beanName);
    if (factoryBean instanceof ScopedProxyFactoryBean) {
        // Pass through - scoped proxy factory beans are a special case and should not
        // be further proxied
    }
    else {
        // It is a candidate FactoryBean - go ahead with enhancement
        return enhanceFactoryBean(factoryBean.getClass(), beanName);
    }
}

If you want to get two different beans, you should change the default scope by @Scope

@Configuration
public class Config {

   @Bean
   @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
   public RandomBean randomBean(){
       return new RandomBean();
   }

   @Bean
   public AnotherBean anotherBean(){
       return new AnotherBean(randomBean()); // this line
   }

}
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
lihongxu
  • 754
  • 5
  • 14
1

According to Spring documentation injection of inter-bean possible only when @Bean method declared within @Configuration. It uses CGLIB and all @Configuration classes are subclassed by it.

Please, check this reference https://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-java-bean-annotation, section '7.12.4 Using the @Configuration annotation'. You will find answer on your question from original source.

eg04lt3r
  • 2,467
  • 14
  • 19