Skip to content

ApplicationListeners cannot be lazy initialized [SPR-4111] #8790

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: duplicateA duplicate of another issuetype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions