需要覆盖属性占位符配置器才能重新加载新的属性
您需要重写方法以使包含哪些属性成为可加载的。这是我的代码processProperties
StringValueResolver
import java.io.IOException;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.PropertyPlaceholderHelper;
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
import org.springframework.util.StringValueResolver;
public class ReloadablePropertyPlaceholderConfigurer
extends PropertyPlaceholderConfigurer {
private ReloadablePlaceholderResolvingStringValueResolver reloadableValueResolver;
public void reloadProperties() throws IOException {
Properties props = mergeProperties();
this.reloadableValueResolver.refreshProperties(props);
}
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
this.reloadableValueResolver = new ReloadablePlaceholderResolvingStringValueResolver(props);
StringValueResolver valueResolver = this.reloadableValueResolver;
this.doProcessProperties(beanFactoryToProcess, valueResolver);
}
private class ReloadablePlaceholderResolvingStringValueResolver
implements StringValueResolver {
private final PropertyPlaceholderHelper helper;
private final ReloadablePropertyPlaceholderConfigurerResolver resolver;
public ReloadablePlaceholderResolvingStringValueResolver(Properties props) {
this.helper = new PropertyPlaceholderHelper(placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
this.resolver = new ReloadablePropertyPlaceholderConfigurerResolver(props);
}
@Override
public String resolveStringValue(String strVal) throws BeansException {
String value = this.helper.replacePlaceholders(strVal, this.resolver);
return (value.equals(nullValue) ? null : value);
}
private void refreshProperties(Properties props){
this.resolver.setProps(props);
}
}
private class ReloadablePropertyPlaceholderConfigurerResolver
implements PlaceholderResolver {
private Properties props;
private ReloadablePropertyPlaceholderConfigurerResolver(Properties props) {
this.props = props;
}
@Override
public String resolvePlaceholder(String placeholderName) {
return ReloadablePropertyPlaceholderConfigurer.this.resolvePlaceholder(placeholderName, props, SYSTEM_PROPERTIES_MODE_FALLBACK);
}
public void setProps(Properties props) {
this.props = props;
}
}
}
以下是.所有这些属性的配置,可以在运行时作为原型Bean重新加载。properties-config.xml
<bean id="propertyConfigurer" class="com.cn21.mail189.analysis.commons.expand.ReloadablePropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<!-- database config -->
<value>classpath:spring/dbconfig.properties</value>
<!-- app config -->
<value>classpath:spring/app.properties</value>
<!-- some other config -->
<value>classpath:xxxx.properties</value>
</list>
</property>
</bean>`