Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring lazy loading - loading one bean loads all @Lazy beans of that class

I've declared two beans of same class type. Initialized them to be @Lazy. @Autowiring one bean of them automatically initialized the other bean as well. I was surprised to see that behavior. Just curious to know more about the mechanism.

Code

//bean
public class HelloWorld {
    public HelloWorld(String msg){
         System.out.println( msg + ", " + this);
    }   
}

@Configuration
@Lazy
public class SpringAppContext {

     @Bean(name="helloworld1")
     public HelloWorld helloworld1(){
        return new HelloWorld("helloworld1");
     }  
     @Bean(name="helloworld2")
     public HelloWorld helloworld2(){
        return new HelloWorld("helloworld2");
     }
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={SpringAppContext.class})
public class SpringBeanLazyLoadTest {
     @Autowired
     private HelloWorld helloworld2;

     @Test // this test is lame but just trying out.
     public void print(){
        System.out.println("Autowired: " + helloworld2);
     }
}

Output

helloworld2, my.entp.spring.HelloWorld@3a9bba
helloworld1, my.entp.spring.HelloWorld@163f7a1 // why was helloworld1 initialized?
Autowired: my.entp.spring.HelloWorld@3a9bba

If you observe the output, you may notice that the helloworld1 bean is initialized when helloworld2 is @Autowired.

I tested by removing @Autowired and it produced expected results: initialized none of the beans.

like image 418
phanin Avatar asked Apr 17 '14 05:04

phanin


3 Answers

When using @Autowired directly, the injection method is byType. In other words, the container sees

 @Autowired
 private HelloWorld helloworld2;

and tries to find a bean of type HelloWorld in the ApplicationContext to inject.

The process of resolving the bean to be injected consists of getting all candidate beans which consists of creating the beans. So the beans being @Lazy doesn't change anything. They will still have to be created in order to be injected.

To clarify M. Deinum's comment on the question, you've given your beans names. For example,

 @Bean(name="helloworld1")

When the injection process takes place, Spring will find all candidate beans that can be injected. If there is more than one, it will filter through them and try to find the best candidate. If it can't, it will throw exceptions. One of the steps for finding a better candidate is comparing the bean name with the name of the target field. Since yours match, the bean named helloworld2 will be chosen.

By removing @Autowired, the beans are never requested from the ApplicationContext and therefore never initialized.

like image 174
Sotirios Delimanolis Avatar answered Sep 28 '22 03:09

Sotirios Delimanolis


From Spring docs

However, when a lazy-initialized bean is a dependency of a singleton bean that is not lazy-initialized, the ApplicationContext creates the lazy-initialized bean at startup, because it must satisfy the singleton's dependencies. The lazy-initialized bean is injected into a singleton bean elsewhere that is not lazy-initialized.

In your test case, the lazy bean is eagerly initialized as the default behaviour of Spring test facility is to prepare the test class instance fully (by injecting all dependencies eagerly) and then hand it over to JUnit. The exact place where it happens is DependencyInjectionTestExecutionListener

protected void injectDependencies(final TestContext testContext) throws Exception {
        Object bean = testContext.getTestInstance();
        AutowireCapableBeanFactory beanFactory = testContext.getApplicationContext().getAutowireCapableBeanFactory();
        beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
        beanFactory.initializeBean(bean, testContext.getTestClass().getName());
        testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
    }
like image 41
Shailendra Avatar answered Sep 28 '22 04:09

Shailendra


Old post but still. Now you can do that:

 @Bean(name="helloworld1", autowire=Autowire.BY_NAME)
 public HelloWorld helloworld1(){
    return new HelloWorld("helloworld1");
 }  
 @Bean(name="helloworld2", autowire=Autowire.BY_NAME)
 public HelloWorld helloworld2(){
    return new HelloWorld("helloworld2");
 }

And/or @Qualifier :

 @Autowired
 @Qualifier("helloworld2")
 private HelloWorld hello;
like image 23
Isaiah Alchemoth Avatar answered Sep 28 '22 05:09

Isaiah Alchemoth