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

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

Introduction

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

Prototype

void resolveAliases(StringValueResolver valueResolver);

Source Link

Document

Resolve all alias target names and aliases registered in this factory, applying the given StringValueResolver to them.

Usage

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  v a2 s. co  m*/
    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: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.
 *///w w  w  .  ja  va 2s  . c  o  m
@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:de.acosix.alfresco.mtsupport.repo.subsystems.TenantAwareSubsystemPlaceholderConfigurer.java

/**
 * {@inheritDoc}/*from w  w  w.j  a v a 2  s.  c  om*/
 */
@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);

}