Skip to content

Instantly share code, notes, and snippets.

@VadimKirilchuk
Last active August 29, 2015 13:57
Show Gist options
  • Save VadimKirilchuk/9435289 to your computer and use it in GitHub Desktop.
Save VadimKirilchuk/9435289 to your computer and use it in GitHub Desktop.
Spring property placeholder configurer which exposes properties
/**
* Use instead of {@link PropertyPlaceholderConfigurer}
* to get access to properties loaded by spring.
* <p>
* Note: there is support to define file with list of properties which should stay unexposed.
*
*/
public class ExposePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer implements InitializingBean {
private Map<String, String> resolvedProps = new TreeMap<String, String>();
private Set<String> hiddenProps = Collections.emptySet();
private PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(DEFAULT_PLACEHOLDER_PREFIX, DEFAULT_PLACEHOLDER_SUFFIX);
private Resource hiddenProperties;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
super.processProperties(beanFactoryToProcess, props);
for (Object propertyKey : props.keySet()) {
String propertyKeyStr = propertyKey.toString();
// filter out properties that should be hidden
if (hiddenProps.contains(propertyKeyStr)) {
continue;
}
resolvedProps.put(propertyKeyStr, helper.replacePlaceholders(props.getProperty(propertyKeyStr), props));
}
}
/**
* @return resolved properties with excluded hidden properties
*/
public Map<String, String> getResolvedProps() {
return Collections.unmodifiableMap(resolvedProps);
}
/**
* Optional hidden properties
* @param hiddenProperties file with properties to hide
*/
public void setHiddenProperties(Resource hiddenProperties) {
this.hiddenProperties = hiddenProperties;
}
public void afterPropertiesSet() throws Exception {
if (hiddenProperties != null) {
InputSupplier<InputStreamReader> supplier = Resources.newReaderSupplier(hiddenProperties.getURL(), Charsets.UTF_8);
hiddenProps = Sets.newHashSet(CharStreams.readLines(supplier));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment