Example usage for org.springframework.beans.factory ListableBeanFactory containsBeanDefinition

List of usage examples for org.springframework.beans.factory ListableBeanFactory containsBeanDefinition

Introduction

In this page you can find the example usage for org.springframework.beans.factory ListableBeanFactory containsBeanDefinition.

Prototype

boolean containsBeanDefinition(String beanName);

Source Link

Document

Check if this bean factory contains a bean definition with the given name.

Usage

From source file:ome.services.graphs.GraphEntry.java

/**
 * Load the spec which has the same name as this entry, but do not load the
 * spec if the name matches {@link #name}. This is called early in the
 * {@link GraphEntry} lifecycle, by {@link GraphSpec}.
 *///w  w  w.  j  av  a2  s .c  om
protected void postProcess(ListableBeanFactory factory) {
    if (name.equals(self.getName())) {
        return;
    } else if (factory.containsBeanDefinition(name)
            && GraphSpec.class.isAssignableFrom(factory.getType(name))) {
        this.subSpec = factory.getBean(name, GraphSpec.class);
        this.subSpec.postProcess(factory);
    }
}

From source file:org.impalaframework.spring.service.SpringServiceBeanUtils.java

/**
 * Checks that the bean with given name contained in the specified bean factory is a singleton.
 * Will return true if bean represented by a bean registered under the scope <code>singletone</code>.
 * If the bean is also a factory bean (see {@link FactoryBean}), then the {@link FactoryBean}
 * instance also needs to be a singleton.
 * /*ww  w.  j  a va2s  .  c om*/
 * Note that in order to work properly the {@link BeanFactory} must be able to recover the {@link BeanDefinition}
 * for a particular bean name. This in particular must mean implementing {@link BeanDefinitionRegistry} or 
 * {@link BeanDefinitionExposing}. In the latter case, if null is returned, then the bean will be treated as a singleton.
 * 
 * @return true if bean is singleton registered bean and, if applicable, a singleton {@link FactoryBean}.
 */
public static boolean isSingleton(BeanFactory beanFactory, String beanName) {

    Assert.notNull(beanFactory, "beanFactory cannot be null");
    Assert.notNull(beanName, "beanName cannot be null");

    boolean singleton = true;

    boolean isBeanFactory = beanFactory.containsBean(BeanFactory.FACTORY_BEAN_PREFIX + beanName);
    if (isBeanFactory) {
        FactoryBean<?> factoryBean = (FactoryBean<?>) beanFactory
                .getBean(BeanFactory.FACTORY_BEAN_PREFIX + beanName);
        singleton = factoryBean.isSingleton();
    }

    if (singleton) {
        //ApplicationContext implements this implements this
        ListableBeanFactory registry = (ListableBeanFactory) beanFactory;

        //we're only interested in top level definitions
        //inner beans won't appear here, so 
        boolean containsBeanDefinition = registry.containsBeanDefinition(beanName);
        if (containsBeanDefinition) {
            BeanDefinition beanDefinition = getBeanDefinition(registry, beanName);

            if (beanDefinition != null) {
                singleton = beanDefinition.isSingleton();
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("Cannot check whether bean definition " + beanName
                        + " is singleton as it is not available as a top level bean");
            }
        }
    }
    return singleton;
}