All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ctrip.framework.apollo.spring.property.SpringValueDefinitionProcessor Maven / Gradle / Ivy

The newest version!
package com.ctrip.framework.apollo.spring.property;

import com.ctrip.framework.apollo.spring.util.SpringInjector;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.TypedStringValue;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;

import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;

/**
 * To process xml config placeholders, e.g.
 *
 * 
 *  <bean class="com.ctrip.framework.apollo.demo.spring.xmlConfigDemo.bean.XmlBean">
 *    <property name="timeout" value="${timeout:200}"/>
 *    <property name="batch" value="${batch:100}"/>
 *  </bean>
 * 
*/ public class SpringValueDefinitionProcessor implements BeanDefinitionRegistryPostProcessor { private static final Map> beanName2SpringValueDefinitions = Maps.newConcurrentMap(); private static final Set PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES = Sets.newConcurrentHashSet(); private final ConfigUtil configUtil; private final PlaceholderHelper placeholderHelper; public SpringValueDefinitionProcessor() { configUtil = ApolloInjector.getInstance(ConfigUtil.class); placeholderHelper = SpringInjector.getInstance(PlaceholderHelper.class); } @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { if (configUtil.isAutoUpdateInjectedSpringPropertiesEnabled()) { processPropertyValues(registry); } } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { } public static Multimap getBeanName2SpringValueDefinitions(BeanDefinitionRegistry registry) { Multimap springValueDefinitions = beanName2SpringValueDefinitions.get(registry); if (springValueDefinitions == null) { springValueDefinitions = LinkedListMultimap.create(); } return springValueDefinitions; } private void processPropertyValues(BeanDefinitionRegistry beanRegistry) { if (!PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES.add(beanRegistry)) { // already initialized return; } if (!beanName2SpringValueDefinitions.containsKey(beanRegistry)) { beanName2SpringValueDefinitions.put(beanRegistry, LinkedListMultimap.create()); } Multimap springValueDefinitions = beanName2SpringValueDefinitions.get(beanRegistry); String[] beanNames = beanRegistry.getBeanDefinitionNames(); for (String beanName : beanNames) { BeanDefinition beanDefinition = beanRegistry.getBeanDefinition(beanName); MutablePropertyValues mutablePropertyValues = beanDefinition.getPropertyValues(); List propertyValues = mutablePropertyValues.getPropertyValueList(); for (PropertyValue propertyValue : propertyValues) { Object value = propertyValue.getValue(); if (!(value instanceof TypedStringValue)) { continue; } String placeholder = ((TypedStringValue) value).getValue(); Set keys = placeholderHelper.extractPlaceholderKeys(placeholder); if (keys.isEmpty()) { continue; } for (String key : keys) { springValueDefinitions.put(beanName, new SpringValueDefinition(key, placeholder, propertyValue.getName())); } } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy