我需要在从DB启动服务器后更新或刷新属性占位符值。 因为我通过在子类中扩展来使用自定义PropertyPlaceholderConfigurer,如下所示。
自定义PropertyPlaceholderConfigurer类:
public class PropertiesSourceImpl extends PropertyPlaceholderConfigurer{
private static final Logger logger = Logger.getLogger(PropertiesSourceImpl.class);
public PropertiesSourceImpl(){ }
private int springSystemPropertiesMode = SYSTEM_PROPERTIES_MODE_OVERRIDE;
private static Map<String, String> propertiesMap;
@Override
public void setSystemPropertiesMode(int systemPropertiesMode) {
String updatedValue = loadFromDB();
System.setProperty("newKey", updatedValue);
super.setSystemPropertiesMode(systemPropertiesMode);
springSystemPropertiesMode = systemPropertiesMode;
}
public void loadProperties(Properties props) throws IOException {
String updatedValue = loadFromDB();
props.setProperty("newKey", updatedValue);
super.loadProperties(props);
propertiesMap = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String valueStr = resolvePlaceholder(keyStr, props);
propertiesMap.put(keyStr, valueStr);
}
}
public static String getProperty(String name) {
return propertiesMap.get(name).toString();
}
public void postProcessBeanFactory(BeanFactory factory){
super.postProcessBeanFactory((ConfigurableListableBeanFactory) factory);
}
}
这是context.xml使用的spring 2.5.6
<?xml version="1.0" encoding="UTF-8" ?>
<bean id="commonsUserManagementPropertyConfigurer" class="com.ebreez.utilities.PropertiesSourceImpl" scope="prototype" >
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="properties">
<props>
<prop key="newKey">no-override</prop>
</props>
</property>
</bean>
<bean name="userManagementService" class="com.ebreez.usermanagement.service.impl.UserManagementServiceImpl"
scope="prototype">
<constructor-arg value="${newKey}" />
</bean>
</beans>
以下是无需重新启动服务器即可更新属性值的代码:
public class myBean{
public void updatePropertValue(){
Properties props=new Properties();
props.setProperty("newKey", "updatedValue");
PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
configurer.setProperties(props);
//configurer.setSystemPropertiesMode(springSystemPropertiesMode);
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"context.xml"}, false);
configurer.setIgnoreUnresolvablePlaceholders(true);
configurer.setSearchSystemEnvironment(false);
//configurer.postProcessBeanFactory(applicationContext.getBeanFactory());
applicationContext.addBeanFactoryPostProcessor(configurer);
applicationContext.refresh();
}
}
但在uerManagementServiceImpl中仍然值 更新一个。
请告诉我代码中的错误,以便为物业占有者设置更新值。