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.
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.