【问题标题】:How to override specific properties loaded from spring PropertyPlaceHolderConfigurer?如何覆盖从 spring PropertyPlaceHolderConfigurer 加载的特定属性?
【发布时间】:2014-09-04 08:10:38
【问题描述】:

我需要在运行时更改使用 spring 的 PropertyPlaceHolderConfigurer 加载的选定配置属性的值。尝试了以下两种解决方案,但解决不了很多,

解决方案 1:在我的配置文件中使用了 PropertyOverrideConfigurer。转介here

但此解决方案仅将值从覆盖的属性文件推送到提到的 bean 的属性。但令人担忧的是,我有很多 bean 指的是这个属性。我只需要在运行时在一个地方覆盖默认值。这鼓励我去寻找下面的第二个解决方案。

解决方案 2:使用提到的 maven surefire 插件here.

但我不打算为此唯一目的使用任何万无一失的方法。是否有任何弹簧配置可以帮助我覆盖测试配置文件中的默认属性值。

谢谢。如果我不够清楚,请告诉我。

【问题讨论】:

  • 拥有一个覆盖属性文件怎么样?
  • 据我了解,我必须使用覆盖属性文件吗?如果是,这类似于 n SOLUTION 1 中提到的内容。或者更确切地说,SPRING 中是否有任何特定机制来覆盖覆盖属性文件中的默认值。
  • 我的建议是使用标准做法,其中 Spring 可以从多个属性文件中读取属性,其中从后面的文件中读取的属性会覆盖从以前的文件中读取的属性。这样的设置在 Spring 中很容易实现。让我知道这是否是您想要的

标签: java spring configuration


【解决方案1】:
You can use the below code to override properties, it will print out all the properties later on you can override whatever property you want:


import java.util.Map;
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.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringPropertiesUtil extends PropertyPlaceholderConfigurer {

    private static Map<String, String> propertiesMap;
    private static String keyToFind = "myProperty";
    // Default as in PropertyPlaceholderConfigurer
    private int springSystemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK;

    @Override
    public void setSystemPropertiesMode(int systemPropertiesMode) {
        super.setSystemPropertiesMode(systemPropertiesMode);
        springSystemPropertiesMode = systemPropertiesMode;
    }

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {

        super.processProperties(beanFactory, props);

        for (Object key : props.keySet()) {
            String keyStr = key.toString();         
            if(keyStr.equals(keyToFind))    {
            String valueStr = resolvePlaceholder(keyStr, props, springSystemPropertiesMode);
            System.out.println(valueStr);      
            }         
        }
    }

    public String getProperty(String name) {
        return propertiesMap.get(name).toString();
    }

    public static void main(String[] args)  {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xm`enter code here`l");

        ((ConfigurableApplicationContext)context).close();
    }

}

【讨论】:

  • 请解释你的答案以及你有什么改变?
  • Gurum 想要覆盖从 PropertyPlaceHolderConfigurer 加载的属性。在我的代码中,我们可以打印/编辑/覆盖 processProperties() 中加载的属性。就是这样。
  • OP 知道这个解决方案,他要求别的东西?
猜你喜欢
  • 2015-10-28
  • 2015-05-23
  • 2011-10-04
  • 2013-08-21
  • 2018-07-21
  • 2016-06-01
  • 2013-07-19
  • 2011-09-19
  • 1970-01-01
相关资源
最近更新 更多