Example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory containsBean

List of usage examples for org.springframework.beans.factory.config ConfigurableListableBeanFactory containsBean

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory containsBean.

Prototype

boolean containsBean(String name);

Source Link

Document

Does this bean factory contain a bean definition or externally registered singleton instance with the given name?

Usage

From source file:org.openmrs.contrib.metadatarepository.webapp.spring.ValidatorExtensionPostProcessor.java

/**
 * Adds the validation configuration files to the list already held in the validator factory bean configuration.
 * @param configurableListableBeanFactory the bean factory
 *//*from  ww w.ja v a 2s .  c om*/
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) {
    if (configurableListableBeanFactory.containsBean(validatorFactoryBeanName)) {
        BeanDefinition validatorFactoryBeanDefinition = configurableListableBeanFactory
                .getBeanDefinition(validatorFactoryBeanName);
        MutablePropertyValues propertyValues = validatorFactoryBeanDefinition.getPropertyValues();
        PropertyValue propertyValue = propertyValues.getPropertyValue("validationConfigLocations");

        //value is expected to be a list.
        List existingValidationConfigLocations = (List) propertyValue.getValue();
        existingValidationConfigLocations.addAll(validationConfigLocations);
    }
}

From source file:alpha.portal.webapp.spring.ValidatorExtensionPostProcessor.java

/**
 * Adds the validation configuration files to the list already held in the
 * validator factory bean configuration.
 * //  ww w .  j  a  v  a 2 s  .  c  om
 * @param configurableListableBeanFactory
 *            the bean factory
 */
public void postProcessBeanFactory(final ConfigurableListableBeanFactory configurableListableBeanFactory) {
    if (configurableListableBeanFactory.containsBean(this.validatorFactoryBeanName)) {
        final BeanDefinition validatorFactoryBeanDefinition = configurableListableBeanFactory
                .getBeanDefinition(this.validatorFactoryBeanName);
        final MutablePropertyValues propertyValues = validatorFactoryBeanDefinition.getPropertyValues();
        final PropertyValue propertyValue = propertyValues.getPropertyValue("validationConfigLocations");

        // value is expected to be a list.
        final List existingValidationConfigLocations = (List) propertyValue.getValue();
        existingValidationConfigLocations.addAll(this.validationConfigLocations);
    }
}

From source file:net.javacrumbs.springws.test.simple.annotation.WsMockControlTestExecutionListener.java

@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
    ConfigurableListableBeanFactory beanFactory = ((AbstractApplicationContext) testContext
            .getApplicationContext()).getBeanFactory();

    if (!beanFactory.containsBean(MOCK_CONTROL_BEAN_NAME)) {
        beanFactory.registerSingleton(MOCK_CONTROL_BEAN_NAME, createWsMockControlFactoryBean());
        beanFactory.registerSingleton(MESSAGE_SENDER_BEAN_NAME, createMessageSender());
        new MockMessageSenderInjector().postProcessBeanFactory(beanFactory);
    }// www .  java  2  s.co  m

}

From source file:org.musicrecital.dao.spring.HibernateExtensionPostProcessor.java

/**
 * Adds the annotated classes and the mapping resources to the existing Session Factory configuration.
 * @param configurableListableBeanFactory the good ol' bean factory
 *//*from  w w  w  .j a  va2 s  .c om*/
@SuppressWarnings("unchecked")
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) {
    if (configurableListableBeanFactory.containsBean(sessionFactoryBeanName)) {
        BeanDefinition sessionFactoryBeanDefinition = configurableListableBeanFactory
                .getBeanDefinition(sessionFactoryBeanName);
        MutablePropertyValues propertyValues = sessionFactoryBeanDefinition.getPropertyValues();

        if (mappingResources != null) {
            // do we have existing resourses?
            PropertyValue propertyValue = propertyValues.getPropertyValue("mappingResources");

            if (propertyValue == null) {
                propertyValue = new PropertyValue("mappingResources", new ArrayList());
                propertyValues.addPropertyValue(propertyValue);
            }

            // value is expected to be a list.
            List existingMappingResources = (List) propertyValue.getValue();
            existingMappingResources.addAll(mappingResources);
        }

        if (annotatedClasses != null) {
            // do we have existing resources?
            PropertyValue propertyValue = propertyValues.getPropertyValue("annotatedClasses");

            if (propertyValue == null) {
                propertyValue = new PropertyValue("annotatedClasses", new ArrayList());
                propertyValues.addPropertyValue(propertyValue);
            }

            // value is expected to be a list.
            List existingMappingResources = (List) propertyValue.getValue();
            existingMappingResources.addAll(annotatedClasses);
        }

        if (configLocations != null) {
            PropertyValue propertyValue = propertyValues.getPropertyValue("configLocations");
            if (propertyValue == null) {
                propertyValue = new PropertyValue("configLocations", new ArrayList());
                propertyValues.addPropertyValue(propertyValue);
            }
            List existingConfigLocations = (List) propertyValue.getValue();
            existingConfigLocations.addAll(configLocations);
        }

        if (hibernateProperties != null) {
            PropertyValue propertyValue = propertyValues.getPropertyValue("hibernateProperties");
            if (propertyValue == null) {
                propertyValue = new PropertyValue("hibernateProperties", new Properties());
                propertyValues.addPropertyValue(propertyValue);
            }
            Properties existingHibernateProperties = (Properties) propertyValue.getValue();
            existingHibernateProperties.putAll(hibernateProperties);
        }
    } else {
        throw new NoSuchBeanDefinitionException(
                "No bean named [" + sessionFactoryBeanName + "] exists within the bean factory. "
                        + "Cannot post process session factory to add Hibernate resource definitions.");
    }
}

From source file:at.porscheinformatik.common.spring.web.extended.annotation.DefaultBeanCondition.java

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(DefaultBean.class.getName());

    Class<?>[] classes = (Class<?>[]) annotationAttributes.get("value");
    ConfigurableListableBeanFactory factory = context.getBeanFactory();

    for (Class<?> c : classes) {
        String[] beanNamesForType = factory.getBeanNamesForType(c, false, false);

        if (beanNamesForType != null) {
            for (String beanName : beanNamesForType) {
                if (factory.containsBean(beanName)) {
                    return false;
                }//from  www. j  a va2  s.co  m
            }
        }
    }

    return true;
}

From source file:org.echocat.jomon.spring.BeanPostConfigurer.java

protected void handleProperty(@Nonnull BeanAndPropertyName beanAndPropertyName,
        @Nonnull AbstractBeanDefinition beanDefinition, @Nullable String value,
        @Nonnull PropertyValueType propertyValueType, @Nonnull ConfigurableListableBeanFactory beanFactory) {
    final MutablePropertyValues propertyValues = getPropertyValues(beanDefinition);
    if (propertyValueType != reference || beanFactory.containsBean(value)) {
        removeOldPropertyIfNeeded(beanAndPropertyName, propertyValues);
        final PropertyValue propertyValue = new PropertyValue(beanAndPropertyName.getPropertyName(),
                propertyValueType == reference ? new RuntimeBeanReference(value) : value);
        propertyValues.addPropertyValue(propertyValue);
    }/* w ww . j a v  a2 s  . c  o  m*/
}

From source file:lodsve.core.condition.OnBeanCondition.java

private boolean containsBean(ConfigurableListableBeanFactory beanFactory, String beanName,
        boolean considerHierarchy) {
    if (considerHierarchy) {
        return beanFactory.containsBean(beanName);
    }/* w w w  .  java  2 s  .  co m*/
    return beanFactory.containsLocalBean(beanName);
}

From source file:org.alfresco.module.org_alfresco_module_rm.security.RMMethodSecurityPostProcessor.java

/**
 * Get all the security bean names by looking at the property values set.
 *
 * @param beanFactory/*from w ww .j  a v a2 s. c  om*/
 * @return
 */
private Set<String> getSecurityBeanNames(ConfigurableListableBeanFactory beanFactory) {
    if (securityBeanNameCache == null) {
        securityBeanNameCache = new HashSet<String>(21);
        if (securityBeanNames != null) {
            securityBeanNameCache.addAll(securityBeanNames);
        }

        for (Object key : properties.keySet()) {
            String[] split = ((String) key).split("\\.");
            int index = split.length - 2;
            String securityBeanName = split[index] + SECURITY_BEAN_POSTFIX;
            if (!securityBeanNameCache.contains(securityBeanName)
                    && beanFactory.containsBean(securityBeanName)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Adding " + securityBeanName + " to list from properties.");
                }

                securityBeanNameCache.add(securityBeanName);
            }
        }
    }

    return securityBeanNameCache;
}

From source file:org.alfresco.util.BeanExtender.java

/**
 * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
 *//*ww  w  . jav a2s . com*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    ParameterCheck.mandatory("beanName", beanName);
    ParameterCheck.mandatory("extendingBeanName", extendingBeanName);

    // check for bean name
    if (!beanFactory.containsBean(beanName)) {
        throw new NoSuchBeanDefinitionException("Can't find bean '" + beanName + "' to be extended.");
    }

    // check for extending bean
    if (!beanFactory.containsBean(extendingBeanName)) {
        throw new NoSuchBeanDefinitionException("Can't find bean '" + extendingBeanName
                + "' that is going to extend original bean definition.");
    }

    // get the bean definitions
    BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
    BeanDefinition extendingBeanDefinition = beanFactory.getBeanDefinition(extendingBeanName);

    // update class
    if (StringUtils.isNotBlank(extendingBeanDefinition.getBeanClassName())
            && !beanDefinition.getBeanClassName().equals(extendingBeanDefinition.getBeanClassName())) {
        beanDefinition.setBeanClassName(extendingBeanDefinition.getBeanClassName());
    }

    // update properties
    MutablePropertyValues properties = beanDefinition.getPropertyValues();
    MutablePropertyValues extendingProperties = extendingBeanDefinition.getPropertyValues();
    for (PropertyValue propertyValue : extendingProperties.getPropertyValueList()) {
        properties.add(propertyValue.getName(), propertyValue.getValue());
    }
}

From source file:org.apereo.portal.utils.cache.EhcacheManagerBeanConfigurer.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    final String[] cacheNames = this.cacheManager.getCacheNames();
    for (final String cacheName : cacheNames) {
        final Ehcache ehcache = this.cacheManager.getEhcache(cacheName);
        this.logger.debug("Registering Ehcache '" + cacheName + "' with bean factory");
        if (beanFactory.containsBean(cacheName)) {
            if (skipDuplicates) {
                continue;
            }//from w w w .  j  a  v a 2s  . c om

            throw new BeanCreationException(
                    "Duplicate Ehcache " + cacheName + " from CacheManager " + cacheManager.getName());
        }

        try {
            beanFactory.registerSingleton(cacheName, ehcache);
        } catch (Exception e) {
            throw new BeanCreationException(
                    "Failed to register Ehcache " + cacheName + " from CacheManager " + cacheManager.getName(),
                    e);
        }
    }
    this.logger.debug("Registered " + cacheNames.length + " Ehcaches with bean factory");
}