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.