Closed
Description
Asaf Romano opened SPR-14611 and commented
Consider the following code in a Config class:
@Bean(name = "sharedExecutorService")
@Primary
@Lazy
public ExecutorService sharedExecutorService()
{
System.out.println("In sharedExecutorService");
ExecutorService executorService = Executors.newFixedThreadPool(5);
System.out.println(String.format("Created: %s", executorService));
return executorService;
}
@Bean(name = "localExecutorService1")
@Lazy
public ExecutorService localExecutorService1()
{
System.out.println("In localExecutorService1");
ExecutorService executorService = sharedExecutorService();
System.out.println(String.format("Created: %s", executorService));
return executorService;
}
@Bean(name = "localExecutorService2")
@Lazy
public ExecutorService localExecutorService2()
{
System.out.println("In localExecutorService2");
ExecutorService executorService = sharedExecutorService();
System.out.println(String.format("Created: %s", executorService));
return executorService;
}
Later on, someone injects the primary bean as follows:
@Inject
private ExecutorService executorService;
The expectation is for the primary bean to be constructed and injected at this point, but for the other two beans not to be constructed yet (as they are lazy). What seems to happen, however, is that all three beans are constructed at this point (albeit the primary bean is successfully injected).
Please note that this issue seems to be quite similar to #18314
Affects: 4.3.2
Issue Links:
- findDefaultEntityManagerFactory should consider EMF bean's primary flag [SPR-7549] #12206 findDefaultEntityManagerFactory should consider EMF bean's primary flag
- BeanFactoryAnnotationUtils.qualifiedBeanOfType should not instantiate all beans of given type before checking qualifier [SPR-13741] #18314 BeanFactoryAnnotationUtils.qualifiedBeanOfType should not instantiate all beans of given type before checking qualifier