Refactor PropertyPlaceholderConfigurer hierarchy
PropertySourcesPlaceholderConfigurer accommodates recent changes in Environment and PropertySource APIs, e.g. no longer assuming enumerability of property names. PSPC reuses as much functionality as possible from AbstractPropertyPlaceholderConfigurer, but overrides postProcessBeanFactory() and defines its own variation on processProperties() in order to accept a PropertyResolver rather than a PropertySource. AbstractPropertyPlaceholderConfigurer introduces doProcessProperties() method to encapsulate that which is actually common, such as the visiting of each bean definition once a StringValueResolver has been created in the subclass.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,18 +16,12 @@
|
||||
|
||||
package org.springframework.beans.factory.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.util.PropertyPlaceholderHelper;
|
||||
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
import org.springframework.util.StringValueResolver;
|
||||
|
||||
|
||||
/**
|
||||
* Abstract base class for property resource configurers that resolve placeholders
|
||||
* in bean definition property values. Implementations <em>pull</em> values from a
|
||||
@@ -114,17 +108,12 @@ public abstract class AbstractPropertyPlaceholderConfigurer extends PropertyReso
|
||||
|
||||
protected boolean ignoreUnresolvablePlaceholders = false;
|
||||
|
||||
private String nullValue;
|
||||
|
||||
private String beanName;
|
||||
protected String nullValue;
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
private String beanName;
|
||||
|
||||
/**
|
||||
* Return the {@code PlaceholderResolver} for this configurer.
|
||||
*/
|
||||
protected abstract PlaceholderResolver getPlaceholderResolver(Properties props);
|
||||
|
||||
/**
|
||||
* Set the prefix that a placeholder string starts with.
|
||||
@@ -200,16 +189,8 @@ public abstract class AbstractPropertyPlaceholderConfigurer extends PropertyReso
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Visit each bean definition in the given bean factory and attempt to replace ${...} property
|
||||
* placeholders with values from the given properties.
|
||||
*/
|
||||
@Override
|
||||
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
|
||||
throws BeansException {
|
||||
|
||||
StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props);
|
||||
protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
|
||||
StringValueResolver valueResolver) {
|
||||
BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);
|
||||
|
||||
String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
|
||||
@@ -234,24 +215,4 @@ public abstract class AbstractPropertyPlaceholderConfigurer extends PropertyReso
|
||||
beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
|
||||
}
|
||||
|
||||
|
||||
private class PlaceholderResolvingStringValueResolver implements StringValueResolver {
|
||||
|
||||
private final PropertyPlaceholderHelper helper;
|
||||
|
||||
private final PlaceholderResolver resolver;
|
||||
|
||||
public PlaceholderResolvingStringValueResolver(Properties props) {
|
||||
this.helper = new PropertyPlaceholderHelper(
|
||||
placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
|
||||
this.resolver = getPlaceholderResolver(props);
|
||||
}
|
||||
|
||||
public String resolveStringValue(String strVal) throws BeansException {
|
||||
String value = this.helper.replacePlaceholders(strVal, this.resolver);
|
||||
return (value.equals(nullValue) ? null : value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -19,11 +19,13 @@ package org.springframework.beans.factory.config;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.core.Constants;
|
||||
import org.springframework.util.PropertyPlaceholderHelper;
|
||||
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
import org.springframework.util.StringValueResolver;
|
||||
|
||||
/**
|
||||
* {@link AbstractPropertyPlaceholderConfigurer} subclass that resolves ${...} placeholders
|
||||
@@ -207,6 +209,37 @@ public class PropertyPlaceholderConfigurer extends AbstractPropertyPlaceholderCo
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Visit each bean definition in the given bean factory and attempt to replace ${...} property
|
||||
* placeholders with values from the given properties.
|
||||
*/
|
||||
@Override
|
||||
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
|
||||
throws BeansException {
|
||||
|
||||
StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props);
|
||||
|
||||
this.doProcessProperties(beanFactoryToProcess, valueResolver);
|
||||
}
|
||||
|
||||
private class PlaceholderResolvingStringValueResolver implements StringValueResolver {
|
||||
|
||||
private final PropertyPlaceholderHelper helper;
|
||||
|
||||
private final PlaceholderResolver resolver;
|
||||
|
||||
public PlaceholderResolvingStringValueResolver(Properties props) {
|
||||
this.helper = new PropertyPlaceholderHelper(
|
||||
placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
|
||||
this.resolver = new PropertyPlaceholderConfigurerResolver(props);
|
||||
}
|
||||
|
||||
public String resolveStringValue(String strVal) throws BeansException {
|
||||
String value = this.helper.replacePlaceholders(strVal, this.resolver);
|
||||
return (value.equals(nullValue) ? null : value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the given String value for placeholder resolution.
|
||||
* @param strVal the String value to parse
|
||||
@@ -225,12 +258,6 @@ public class PropertyPlaceholderConfigurer extends AbstractPropertyPlaceholderCo
|
||||
return helper.replacePlaceholders(strVal, resolver);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PlaceholderResolver getPlaceholderResolver(Properties props) {
|
||||
return new PropertyPlaceholderConfigurerResolver(props);
|
||||
}
|
||||
|
||||
|
||||
private class PropertyPlaceholderConfigurerResolver implements PlaceholderResolver {
|
||||
|
||||
private final Properties props;
|
||||
@@ -244,5 +271,4 @@ public class PropertyPlaceholderConfigurer extends AbstractPropertyPlaceholderCo
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user