Search in sources :

Example 6 with BeanDefinitionVisitor

use of org.springframework.beans.factory.config.BeanDefinitionVisitor in project com.revolsys.open by revolsys.

the class BeanConfigurrer method processPlaceholderAttributes.

protected void processPlaceholderAttributes(final ConfigurableListableBeanFactory beanFactory, final String beanName, final Map<String, Object> attributes) throws BeansException {
    final StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver("${", "}", this.ignoreUnresolvablePlaceholders, null, attributes);
    final BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);
    // locations.
    if (!(beanName.equals(this.beanName) && beanFactory.equals(this.applicationContext))) {
        final BeanDefinition bd = beanFactory.getBeanDefinition(beanName);
        try {
            visitor.visitBeanDefinition(bd);
        } catch (final BeanDefinitionStoreException ex) {
            throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName, ex.getMessage());
        }
    }
    // NEW in Spring 2.5: resolve placeholders in alias target names and aliases
    // as well.
    beanFactory.resolveAliases(valueResolver);
}
Also used : StringValueResolver(org.springframework.util.StringValueResolver) PlaceholderResolvingStringValueResolver(com.revolsys.spring.util.PlaceholderResolvingStringValueResolver) PlaceholderResolvingStringValueResolver(com.revolsys.spring.util.PlaceholderResolvingStringValueResolver) BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition)

Example 7 with BeanDefinitionVisitor

use of org.springframework.beans.factory.config.BeanDefinitionVisitor in project com.revolsys.open by revolsys.

the class BeanConfigurrer method processPlaceholderAttributes.

protected void processPlaceholderAttributes(final ConfigurableListableBeanFactory beanFactory, final Map<String, Object> attributes) throws BeansException {
    final Map<String, Object> attributeMap = new LinkedHashMap<>();
    for (final Entry<String, Object> entry : attributes.entrySet()) {
        final String key = entry.getKey();
        final Object value = entry.getValue();
        if (!(value instanceof BeanReference)) {
            attributeMap.put(key, value);
        }
    }
    final StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver("${", "}", this.ignoreUnresolvablePlaceholders, null, attributeMap);
    final BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);
    final String[] beanNames = beanFactory.getBeanDefinitionNames();
    for (int i = 0; i < beanNames.length; i++) {
        // locations.
        if (!(beanNames[i].equals(this.beanName) && beanFactory.equals(this.applicationContext))) {
            final BeanDefinition bd = beanFactory.getBeanDefinition(beanNames[i]);
            try {
                visitor.visitBeanDefinition(bd);
            } catch (final 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.
    beanFactory.resolveAliases(valueResolver);
}
Also used : PlaceholderResolvingStringValueResolver(com.revolsys.spring.util.PlaceholderResolvingStringValueResolver) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) LinkedHashMap(java.util.LinkedHashMap) StringValueResolver(org.springframework.util.StringValueResolver) PlaceholderResolvingStringValueResolver(com.revolsys.spring.util.PlaceholderResolvingStringValueResolver) BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor) BeanReference(com.revolsys.spring.BeanReference)

Example 8 with BeanDefinitionVisitor

use of org.springframework.beans.factory.config.BeanDefinitionVisitor in project ff4j by ff4j.

the class FF4jPropertiesPlaceHolderConfigurer method postProcessBeanFactory.

/**
 * {@inheritDoc}
 */
public final void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) {
    try {
        // 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);
    }
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) BeansException(org.springframework.beans.BeansException) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException)

Example 9 with BeanDefinitionVisitor

use of org.springframework.beans.factory.config.BeanDefinitionVisitor in project grails-core by grails.

the class GrailsPlaceholderConfigurer method doProcessProperties.

@Override
protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess, StringValueResolver valueResolver) {
    BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver) {

        @Override
        protected void visitMap(Map<?, ?> mapVal) {
            if (mapVal instanceof Config)
                return;
            super.visitMap(mapVal);
        }
    };
    String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    for (String curName : beanNames) {
        // to avoid failing on unresolvable placeholders in properties file locations.
        if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
            BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);
            try {
                visitor.visitBeanDefinition(bd);
            } catch (Exception ex) {
                throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage(), ex);
            }
        }
    }
    // 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);
}
Also used : BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) Config(grails.config.Config) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Map(java.util.Map) IOException(java.io.IOException) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException)

Aggregations

BeanDefinitionVisitor (org.springframework.beans.factory.config.BeanDefinitionVisitor)9 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)7 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)5 StringValueResolver (org.springframework.util.StringValueResolver)5 PlaceholderResolvingStringValueResolver (com.revolsys.spring.util.PlaceholderResolvingStringValueResolver)2 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)2 BeanReference (com.revolsys.spring.BeanReference)1 Config (grails.config.Config)1 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 BeansException (org.springframework.beans.BeansException)1 BeanInitializationException (org.springframework.beans.factory.BeanInitializationException)1