How to get the set of beans that are to be created in Spring?
Posted
by cyborg
on Stack Overflow
See other posts from Stack Overflow
or by cyborg
Published on 2010-06-01T17:54:50Z
Indexed on
2010/06/02
12:43 UTC
Read the original article
Hit count: 223
So here's the scenario:
I have a Spring XML configuration with some lazy-beans, some not lazy-beans and some beans that depend on other beans. Eventually Spring will resolve all this so that only the beans that are meant to be created are created.
The question: how can I programmatically tell what this set is?
When I use context.getBean(name)
that initializes the bean. BeanDefinition.isLazyInit()
will only tell me how I defined the bean.
Any other ideas?
ETA:
In DefaultListableBeanFactory
:
public void preInstantiateSingletons() throws BeansException {
if (this.logger.isInfoEnabled()) {
this.logger.info("Pre-instantiating singletons in " + this);
}
synchronized (this.beanDefinitionMap) {
for (Iterator it = this.beanDefinitionNames.iterator(); it.hasNext();) {
String beanName = (String) it.next();
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
if (isFactoryBean(beanName)) {
FactoryBean factory = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName);
if (factory instanceof SmartFactoryBean && ((SmartFactoryBean) factory).isEagerInit()) {
getBean(beanName);
}
}
else {
getBean(beanName);
}
}
}
}
}
The set of instantiable beans is initialized. When initializing this set any beans not in this set referenced by this set will also be created. From looking through the source it does not look like there's going to be any easy way to answer my question.
© Stack Overflow or respective owner