Search in sources :

Example 1 with BeanDefinitionVisitor

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

the class ReloadingPropertyPlaceholderConfigurer method processProperties.

/**
 * copy & paste, just so we can insert our own visitor.
 * 启动时 进行配置的解析
 */
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
    BeanDefinitionVisitor visitor = new ReloadingPropertyPlaceholderConfigurer.PlaceholderResolvingBeanDefinitionVisitor(props);
    String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    for (int i = 0; i < beanNames.length; i++) {
        // .unicon.iamlabs.spring.properties file locations.
        if (!(beanNames[i].equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
            this.currentBeanName = beanNames[i];
            try {
                BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanNames[i]);
                try {
                    visitor.visitBeanDefinition(bd);
                } catch (BeanDefinitionStoreException ex) {
                    throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanNames[i], ex.getMessage());
                }
            } finally {
                currentBeanName = null;
            }
        }
    }
    StringValueResolver stringValueResolver = new PlaceholderResolvingStringValueResolver(props);
    // New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
    beanFactoryToProcess.resolveAliases(stringValueResolver);
    // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
    beanFactoryToProcess.addEmbeddedValueResolver(stringValueResolver);
}
Also used : StringValueResolver(org.springframework.util.StringValueResolver) BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 2 with BeanDefinitionVisitor

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

the class LightConfFactory method processProperties.

@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, ConfigurablePropertyResolver propertyResolver) throws BeansException {
    // super.processProperties(beanFactoryToProcess, propertyResolver);
    // light conf BeanDefinitionVisitor
    BeanDefinitionVisitor lightConfVisitor = getLightConfBeanDefinitionVisitor();
    // visit bean definition
    String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    if (beanNames != null && beanNames.length > 0) {
        for (String beanName : beanNames) {
            if (!(beanName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
                // XML and @Value:resolves '${...}' placeholders within bean definition property values
                BeanDefinition beanDefinition = beanFactoryToProcess.getBeanDefinition(beanName);
                lightConfVisitor.visitBeanDefinition(beanDefinition);
                // Annotation:resolves '@LightConf' annotations within bean definition fields
                final Object beanWithLightConf = beanFactoryToProcess.getBean(beanName);
                // refresh bean with light conf
                refreshBeanWithLightConf(beanWithLightConf, null);
            }
        }
    }
    logger.info(">>>>>>>>>>> light-conf, lightConfFactory process success");
}
Also used : BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 3 with BeanDefinitionVisitor

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

the class LightConfFactory method getLightConfBeanDefinitionVisitor.

/**
 * light conf BeanDefinitionVisitor
 *
 * @return
 */
private static BeanDefinitionVisitor getLightConfBeanDefinitionVisitor() {
    // light conf StringValueResolver
    StringValueResolver lightConfValueResolver = new StringValueResolver() {

        String placeholderPrefix = "${";

        String placeholderSuffix = "}";

        @Override
        public String resolveStringValue(String strVal) {
            StringBuffer buf = new StringBuffer(strVal);
            // loop replace by light-conf, if the value match '${***}'
            boolean start = strVal.startsWith(placeholderPrefix);
            boolean end = strVal.endsWith(placeholderSuffix);
            while (start && end) {
                // replace by light-conf
                String key = buf.substring(placeholderPrefix.length(), buf.length() - placeholderSuffix.length());
                String value = LightConfClient.get(key, "");
                buf = new StringBuffer(value);
                logger.info(">>>>>>>>>>> light-conf, resolved placeholder success, [{}={}]", key, value);
                start = buf.toString().startsWith(placeholderPrefix);
                end = buf.toString().endsWith(placeholderSuffix);
            }
            return buf.toString();
        }
    };
    // light conf BeanDefinitionVisitor
    BeanDefinitionVisitor lightConfVisitor = new BeanDefinitionVisitor(lightConfValueResolver);
    return lightConfVisitor;
}
Also used : StringValueResolver(org.springframework.util.StringValueResolver) BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor)

Example 4 with BeanDefinitionVisitor

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

the class XxlConfFactory method processProperties.

@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, ConfigurablePropertyResolver propertyResolver) throws BeansException {
    // super.processProperties(beanFactoryToProcess, propertyResolver);
    // xxl conf BeanDefinitionVisitor
    BeanDefinitionVisitor xxlConfVisitor = getXxlConfBeanDefinitionVisitor();
    // visit bean definition
    String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
    if (beanNames != null && beanNames.length > 0) {
        for (String beanName : beanNames) {
            if (!(beanName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
                // XML:resolves '${...}' placeholders within bean definition property values
                BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(beanName);
                xxlConfVisitor.visitBeanDefinition(bd);
                // Annotation:resolves '@XxlConf' annotations within bean definition fields
                final Object beanWithXxlConf = beanFactoryToProcess.getBean(beanName);
                // refresh bean with xxl conf
                refreshBeanWithXxlConf(beanWithXxlConf, null);
            }
        }
    }
    logger.info(">>>>>>>>>>> xxl conf, XxlConfFactory process success");
}
Also used : BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition)

Example 5 with BeanDefinitionVisitor

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

the class XxlConfFactory method getXxlConfBeanDefinitionVisitor.

/**
 * xxl conf BeanDefinitionVisitor
 *
 * @return
 */
private static BeanDefinitionVisitor getXxlConfBeanDefinitionVisitor() {
    // xxl conf StringValueResolver
    StringValueResolver xxlConfValueResolver = new StringValueResolver() {

        String placeholderPrefix = "${";

        String placeholderSuffix = "}";

        @Override
        public String resolveStringValue(String strVal) {
            StringBuffer buf = new StringBuffer(strVal);
            // loop replace by xxl-conf, if the value match '${***}'
            boolean start = strVal.startsWith(placeholderPrefix);
            boolean end = strVal.endsWith(placeholderSuffix);
            while (start && end) {
                // replace by xxl-conf
                String key = buf.substring(placeholderPrefix.length(), buf.length() - placeholderSuffix.length());
                String zkValue = XxlConfClient.get(key, "");
                buf = new StringBuffer(zkValue);
                logger.info(">>>>>>>>>>> xxl conf, resolved placeholder success, [{}={}]", key, zkValue);
                start = buf.toString().startsWith(placeholderPrefix);
                end = buf.toString().endsWith(placeholderSuffix);
            }
            return buf.toString();
        }
    };
    // xxl conf BeanDefinitionVisitor
    BeanDefinitionVisitor xxlConfVisitor = new BeanDefinitionVisitor(xxlConfValueResolver);
    return xxlConfVisitor;
}
Also used : StringValueResolver(org.springframework.util.StringValueResolver) BeanDefinitionVisitor(org.springframework.beans.factory.config.BeanDefinitionVisitor)

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