With the direct use of the @Autowired injection byType . In other words, the container sees
@Autowired private HelloWorld helloworld2;
and tries to find a bean of type HelloWorld in the ApplicationContext for input.
The process of resolving the bean to be entered consists in obtaining the entire beans candidate, which consists of creating beans. So beans @Lazy doesn't change anything. They must still be created in order to be administered.
To clarify M. Deinum to the question, you gave your names beans. For instance,
@Bean(name="helloworld1")
When the injection process takes place, Spring will find all the candidate beans that can be injected. If there are more than one, he will filter them and try to find the best candidate. If it cannot, it will throw exceptions. One of the steps to finding the best candidate is to compare the name of the bean with the name of the target field. After your match, a bean named helloworld2 will be selected.
By removing @Autowired , beans is never requested with ApplicationContext and therefore never initialized.
source share