Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在服务器启动后更新spring属性placeholder值_Spring - Fatal编程技术网

如何在服务器启动后更新spring属性placeholder值

如何在服务器启动后更新spring属性placeholder值,spring,Spring,从DB启动服务器后,我需要更新或刷新属性占位符值。 正如我在下面的子类中扩展使用自定义PropertyPlaceHolderConfigure一样 自定义属性PlaceHolderConfiguration类: public class PropertiesSourceImpl extends PropertyPlaceholderConfigurer{ private static final Logger logger = Logger.getLogger(PropertiesSo

从DB启动服务器后,我需要更新或刷新属性占位符值。 正如我在下面的子类中扩展使用自定义PropertyPlaceHolderConfigure一样

自定义属性PlaceHolderConfiguration类:

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);
    }
}
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中的值


请让我知道我的代码中有什么错误,以便为属性占位符设置更新的值。

如何检查属性是否已更新?要检查值,需要在UserManagementServiceImpl.java的构造函数中添加getNewKey()
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();
}
}