Example usage for org.springframework.beans.factory.config BeanDefinitionVisitor visitBeanDefinition

List of usage examples for org.springframework.beans.factory.config BeanDefinitionVisitor visitBeanDefinition

Introduction

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

Prototype

public void visitBeanDefinition(BeanDefinition beanDefinition) 

Source Link

Document

Traverse the given BeanDefinition object and the MutablePropertyValues and ConstructorArgumentValues contained in them.

Usage

From source file:org.brekka.stillingar.spring.pc.ConfigurationPlaceholderConfigurer.java

/**
 * Copied in its entirety from the {@link PropertyPlaceholderConfigurer} method of the same name. The only changes
 * are to the valueResolver and BeanDefinitionVisitor instances.
 *///from  w  w w.  j  a  v  a  2  s  .  c om
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactoryToProcess) throws BeansException {

    String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    for (String curName : beanNames) {
        CustomStringValueResolver valueResolver = new CustomStringValueResolver(this.placeholderParser,
                this.configurationSource, this.beanFactory);

        // Check that we're not parsing our own bean definition,
        // to avoid failing on unresolvable placeholders in properties file
        // locations.
        if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
            BeanDefinition beanDef = beanFactoryToProcess.getBeanDefinition(curName);
            try {
                BeanDefinitionVisitor visitor = new CustomBeanDefinitionVisitor(curName, beanDef.isSingleton(),
                        valueResolver);
                visitor.visitBeanDefinition(beanDef);
            } catch (Exception ex) {
                throw new BeanDefinitionStoreException(beanDef.getResourceDescription(), curName, ex);
            }
        }
    }

    StringValueResolver valueResolver = new CustomStringValueResolver(this.placeholderParser,
            this.configurationSource, this.beanFactory);

    // New in Spring 2.5: resolve placeholders in alias target names and
    // aliases as well.
    beanFactoryToProcess.resolveAliases(valueResolver);

    // New in Spring 3.0: resolve placeholders in embedded values such as
    // annotation attributes.
    beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
}

From source file:org.ff4j.spring.placeholder.FF4jPropertiesPlaceHolderConfigurer.java

/** {@inheritDoc} */
public final void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) {
    try {// www.  ja v a  2  s .com
        // 1) Retrieve properties from ff4j
        BeanDefinitionVisitor visitor = new PropertiesPlaceHolderBeanDefinitionVisitor(ff4j);

        // 2) Inject property & features value
        String[] beanNames = beanFactory.getBeanDefinitionNames();
        for (int i = 0; i < beanNames.length; i++) {
            if (beanNames[i].equals(beanName)) {
                continue;
            }
            BeanDefinition bd = beanFactory.getBeanDefinition(beanNames[i]);
            try {
                visitor.visitBeanDefinition(bd);
            } catch (BeanDefinitionStoreException ex) {
                throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i],
                        ex.getMessage());
            }
        }
    } catch (Exception e) {
        LOGGER.error("Cannot handle placeholding through ff4j : ", e);
        throw new BeanInitializationException("An error occured during substition", e);
    }
}

From source file:com.devbury.desktoplib.spring.LogFilterPropertyPlaceholderConfigurer.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory bf) throws BeansException {
    BeanDefinitionVisitor visitor = newVisitor();
    String[] beanNames = bf.getBeanDefinitionNames();
    for (int i = 0; i < beanNames.length; i++) {
        // Check that we're not parsing our own bean definition,
        // to avoid failing on unresolvable placeholders in properties file
        // locations.
        if (!(beanNames[i].equals(beanName))) {
            BeanDefinition bd = bf.getBeanDefinition(beanNames[i]);
            try {
                visitor.visitBeanDefinition(bd);
            } catch (BeanDefinitionStoreException ex) {
                throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i],
                        ex.getMessage());
            }//from   w  ww.  java  2s  .  co  m
        }
    }
}

From source file:org.apache.servicemix.document.factory.DocumentFactory.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactoryToProcess) throws BeansException {
    BeanDefinitionVisitor visitor = new BundleExtUrlBeanDefinitionVisitor();
    String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    for (int i = 0; i < beanNames.length; i++) {
        // Check that we're not parsing our own bean definition,
        // to avoid failing on unresolvable placeholders in properties file locations.
        if (!(beanNames[i].equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
            BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanNames[i]);
            try {
                visitor.visitBeanDefinition(bd);
            } catch (BeanDefinitionStoreException ex) {
                throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i],
                        ex.getMessage());
            }// w w  w.  j a v  a 2s  . com
        }
    }
}

From source file:com.photon.phresco.configuration.XmlPropertyPlaceholderConfigurer.java

@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
        throws BeansException {

    org.springframework.util.StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(
            props);/*from w  w w .  j a  va 2  s . com*/
    org.springframework.beans.factory.config.BeanDefinitionVisitor visitor = new org.springframework.beans.factory.config.BeanDefinitionVisitor(
            valueResolver);

    String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    for (int i = 0; i < beanNames.length; i++) {
        // Check that we're not parsing our own bean definition,
        // to avoid failing on unresolvable placeholders in properties file locations.
        if (!(beanNames[i].equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
            BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanNames[i]);
            try {
                visitor.visitBeanDefinition(bd);
            } catch (BeanDefinitionStoreException ex) {
                throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i],
                        ex.getMessage());
            }
        }
    }

    // New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
    beanFactoryToProcess.resolveAliases(valueResolver);
}

From source file:de.acosix.alfresco.mtsupport.repo.subsystems.TenantAwareSubsystemPlaceholderConfigurer.java

/**
 * {@inheritDoc}//  ww  w. j a va  2 s  . c o  m
 */
@Override
protected void doProcessProperties(final ConfigurableListableBeanFactory beanFactoryToProcess,
        final StringValueResolver valueResolver) {
    final BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver) {

        /**
         * {@inheritDoc}
         */
        @Override
        protected Object resolveValue(final Object value) {
            Object result = value;

            // TODO Report bug with Spring
            // TypedStringValue may be reused as result of cloneBeanDefinition and thus should not be mutated

            if (value instanceof TypedStringValue) {
                final TypedStringValue typedStringValue = (TypedStringValue) value;
                final String stringValue = typedStringValue.getValue();
                if (stringValue != null) {
                    final String visitedString = this.resolveStringValue(stringValue);
                    if (!stringValue.equals(visitedString)) {
                        result = typedStringValue.hasTargetType()
                                ? new TypedStringValue(visitedString, typedStringValue.getTargetType())
                                : new TypedStringValue(visitedString);
                    }
                }
            } else {
                result = super.resolveValue(value);
            }

            return result;
        }
    };

    final String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    for (final String curName : beanNames) {
        // Check that we're not parsing our own bean definition,
        // to avoid failing on unresolvable placeholders in properties file locations.
        if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
            final String tenantDomain;
            if (curName.contains(TenantBeanUtils.TENANT_BEAN_NAME_PATTERN)) {
                tenantDomain = curName.substring(curName.indexOf(TenantBeanUtils.TENANT_BEAN_NAME_PATTERN)
                        + TenantBeanUtils.TENANT_BEAN_NAME_PATTERN.length());
                LOGGER.debug("[{}] Processing bean {} for tenant domain {}", this.beanName, curName,
                        tenantDomain);
            } else {
                LOGGER.debug("[{}] Processing bean {} without tenant domain", this.beanName, curName);
                tenantDomain = null;
            }

            final BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);
            TENANT_CONTEXT.set(tenantDomain);
            try {
                visitor.visitBeanDefinition(bd);
            } catch (final Exception ex) {
                throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage());
            } finally {
                TENANT_CONTEXT.remove();
            }
        }
    }
    LOGGER.debug("[{}] Completed processing all beans", this.beanName);

    // New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
    beanFactoryToProcess.resolveAliases(valueResolver);

    // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
    beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);

}