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

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

Introduction

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

Prototype

Object configureBean(Object existingBean, String beanName) throws BeansException;

Source Link

Document

Configure the given raw bean: autowiring bean properties, applying bean property values, applying factory callbacks such as setBeanName and setBeanFactory , and also applying all bean post processors (including ones which might wrap the given raw bean).

Usage

From source file:org.alfresco.repo.management.subsystems.LegacyConfigPostProcessor.java

@SuppressWarnings("unchecked")
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    try {//from  www.j  a  v a2s .c o m
        // Look up the global-properties bean and its locations list
        MutablePropertyValues globalProperties = beanFactory
                .getBeanDefinition(LegacyConfigPostProcessor.BEAN_NAME_GLOBAL_PROPERTIES).getPropertyValues();
        PropertyValue pv = globalProperties.getPropertyValue(LegacyConfigPostProcessor.PROPERTY_LOCATIONS);
        Collection<Object> globalPropertyLocations;
        Object value;

        // Use the locations list if there is one, otherwise associate a new empty list
        if (pv != null && (value = pv.getValue()) != null && value instanceof Collection) {
            globalPropertyLocations = (Collection<Object>) value;
        } else {
            globalPropertyLocations = new ManagedList(10);
            globalProperties.addPropertyValue(LegacyConfigPostProcessor.PROPERTY_LOCATIONS,
                    globalPropertyLocations);
        }

        // Move location paths added to repository-properties
        MutablePropertyValues repositoryProperties = processLocations(beanFactory, globalPropertyLocations,
                LegacyConfigPostProcessor.BEAN_NAME_REPOSITORY_PROPERTIES,
                new String[] { "classpath:alfresco/version.properties" });
        // Fix up additional properties to enforce correct order of precedence
        repositoryProperties.addPropertyValue("ignoreUnresolvablePlaceholders", Boolean.TRUE);
        repositoryProperties.addPropertyValue("localOverride", Boolean.FALSE);
        repositoryProperties.addPropertyValue("valueSeparator", null);
        repositoryProperties.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_NEVER");

        // Move location paths added to hibernateConfigProperties
        MutablePropertyValues hibernateProperties = processLocations(beanFactory, globalPropertyLocations,
                LegacyConfigPostProcessor.BEAN_NAME_HIBERNATE_PROPERTIES,
                new String[] { "classpath:alfresco/domain/hibernate-cfg.properties",
                        "classpath*:alfresco/enterprise/cache/hibernate-cfg.properties" });
        // Fix up additional properties to enforce correct order of precedence
        hibernateProperties.addPropertyValue("localOverride", Boolean.TRUE);

        // Because Spring gets all post processors in one shot, the bean may already have been created. Let's try to
        // fix it up!
        PropertyPlaceholderConfigurer repositoryConfigurer = (PropertyPlaceholderConfigurer) beanFactory
                .getSingleton(LegacyConfigPostProcessor.BEAN_NAME_REPOSITORY_PROPERTIES);
        if (repositoryConfigurer != null) {
            // Reset locations list
            repositoryConfigurer.setLocations(null);

            // Invalidate cached merged bean definitions
            ((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(
                    LegacyConfigPostProcessor.BEAN_NAME_REPOSITORY_PROPERTIES,
                    beanFactory.getBeanDefinition(LegacyConfigPostProcessor.BEAN_NAME_REPOSITORY_PROPERTIES));

            // Reconfigure the bean according to its new definition
            beanFactory.configureBean(repositoryConfigurer,
                    LegacyConfigPostProcessor.BEAN_NAME_REPOSITORY_PROPERTIES);
        }
    } catch (NoSuchBeanDefinitionException e) {
        // Ignore and continue
    }
}

From source file:org.springframework.beans.factory.wiring.BeanConfigurerSupport.java

/**
 * Configure the bean instance./*w w  w .  j  av a  2 s  .  c  om*/
 * <p>Subclasses can override this to provide custom configuration logic.
 * Typically called by an aspect, for all bean instances matched by a pointcut.
 * @param beanInstance the bean instance to configure (must <b>not</b> be {@code null})
 */
public void configureBean(Object beanInstance) {
    if (this.beanFactory == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("BeanFactory has not been set on " + ClassUtils.getShortName(getClass()) + ": "
                    + "Make sure this configurer runs in a Spring container. Unable to configure bean of type ["
                    + ClassUtils.getDescriptiveType(beanInstance) + "]. Proceeding without injection.");
        }
        return;
    }

    BeanWiringInfoResolver bwiResolver = this.beanWiringInfoResolver;
    Assert.state(bwiResolver != null, "No BeanWiringInfoResolver available");
    BeanWiringInfo bwi = bwiResolver.resolveWiringInfo(beanInstance);
    if (bwi == null) {
        // Skip the bean if no wiring info given.
        return;
    }

    ConfigurableListableBeanFactory beanFactory = this.beanFactory;
    Assert.state(beanFactory != null, "No BeanFactory available");
    try {
        if (bwi.indicatesAutowiring() || (bwi.isDefaultBeanName() && bwi.getBeanName() != null
                && !beanFactory.containsBean(bwi.getBeanName()))) {
            // Perform autowiring (also applying standard factory / post-processor callbacks).
            beanFactory.autowireBeanProperties(beanInstance, bwi.getAutowireMode(), bwi.getDependencyCheck());
            beanFactory.initializeBean(beanInstance, bwi.getBeanName());
        } else {
            // Perform explicit wiring based on the specified bean definition.
            beanFactory.configureBean(beanInstance, bwi.getBeanName());
        }
    } catch (BeanCreationException ex) {
        Throwable rootCause = ex.getMostSpecificCause();
        if (rootCause instanceof BeanCurrentlyInCreationException) {
            BeanCreationException bce = (BeanCreationException) rootCause;
            String bceBeanName = bce.getBeanName();
            if (bceBeanName != null && beanFactory.isCurrentlyInCreation(bceBeanName)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Failed to create target bean '" + bce.getBeanName()
                            + "' while configuring object of type [" + beanInstance.getClass().getName()
                            + "] - probably due to a circular reference. This is a common startup situation "
                            + "and usually not fatal. Proceeding without injection. Original exception: " + ex);
                }
                return;
            }
        }
        throw ex;
    }
}