Description
FoX opened SPR-4111 and commented
All ApplicationListeners will be instantiated when the ApplicationContext registers its listeners.
The consequence of this operation for our application is that nearly all beans are initialized at startup time eventhough lazy-init is set to true on all contexts.
This is especially annoying in our integration tests, because the initialization takes a lot of time.
Isn't there a possibility to register these listeners as a proxy, allowing lazy initialization of these listeners until an application event occurs?
Or, maybe it would also be a valid solution to not put the listeners themselves in the map, but the bean names (not that nice, as it would break observer/observable pattern)
It seems that the getBeansOfType() method in the DefaultListableBeanFactory doesn't take the allowEagerInit boolean into account when retrieving the beans:
public Map getBeansOfType(Class type, boolean includePrototypes, boolean allowEagerInit) throws BeansException {
String[] beanNames = getBeanNamesForType(type, includePrototypes, allowEagerInit);
Map result = CollectionFactory.createLinkedMapIfPossible(beanNames.length);
for (int i = 0; i < beanNames.length; i++) {
String beanName = beanNames[i];
try {
// -----------------------> getBean(beanName) will always instantiate the bean, eager init or not?
result.put(beanName, getBean(beanName));
}
...
}
...
}
Affects: 2.0.7
Issue Links:
- ApplicationListener beans eagerly instantiated even when marked as lazy-init [SPR-4054] #8733 ApplicationListener beans eagerly instantiated even when marked as lazy-init ("duplicates")