java - 使用带有变量的配置/属性文件

标签 java variables configuration import configuration-files

我使用像这样的简单文本文件

BMG-P   (someLongComplicatedExpression)(.*P)
BMG T   (someLongComplicatedExpression)(.*[Tt])
BMG MPA (someLongComplicatedExpression)(.*MPA)

配置我的应用程序(使用 bufferedReader.readLine().split("\t") 进行简单导入)。困扰我的是冗余

我正在考虑这样的解决方案:

%s=(someLongComplicatedExpression)
BMG-P   %s(.*P)
BMG T   %s(.*[Tt])
BMG MPA %s(.*MPA)

我在其中读取变量的值(例如 %s),然后在导入后替换它们在字符串中出现的位置。

我的问题是:

  • 您知道哪些替代方法?
  • 在代码中实现变量替换的简单方法是什么?
  • 您能给我指出任何支持此类属性文件的框架吗?

最佳答案

我为 Java Properties 类编写了这个简单的扩展:

import java.io.Serializable;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Allows properties to contain expansions of the form ${propertyName}. This
 * class makes no attempt to detect circular references, so be careful.
 */
public class ExpandingProperties extends Properties implements PropertySource {

    private static final long serialVersionUID = 259782782423517925L;
    private final Expander expander = new Expander();

    @Override
    public String getProperty(String key) {
        return expander.expand(super.getProperty(key), this);
    }
}

class Expander implements Serializable {

    private static final long serialVersionUID = -2229337918353092460L;
    private final Pattern pattern = Pattern.compile("\\$\\{([^}]+)\\}");

    /**
     * Expands variables of the form "${variableName}" within the
     * specified string, using the property source to lookup the
     * relevant value.
     */
    public String expand(final String s, final PropertySource propertySource) {
        if (s == null) {
            return null;
        }
        final StringBuffer sb = new StringBuffer();
        final Matcher matcher = pattern.matcher(s);
        while (matcher.find()) {
            final String variableName = matcher.group(1);
            final String value = propertySource.getProperty(variableName);
            if (value == null) {
                throw new RuntimeException("No property found for: " + variableName);
            }
            matcher.appendReplacement(sb, value.replace("$", "\\$"));
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}

interface PropertySource {

    String getProperty(String key);
}

使用示例:

public static void main(String[] args) {
    Properties properties = new ExpandingProperties();
    properties.put("myVar", "myLongExpression");
    properties.put("foo", "${myVar}_1");
    properties.put("bar", "${foo}_abc");

    System.out.println(properties.getProperty("bar"));
}

打印:

myLongExpression_1_abc

由于 ExpandingPropertiesProperties 的扩展,它继承了所有用于从属性文件加载值的 load...() 方法。

替代方案是 EProperties它与上面的代码做了类似的事情,但更进一步,允许您嵌套属性文件等。我发现它对于我所需要的东西来说太过分了。

关于java - 使用带有变量的配置/属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11901530/

相关文章:

java - 编辑引用的项目配置而不重建

Java Web 服务使用者 > 找不到名为 'null' 的客户端传输

php - 取消设置变量组

javascript - 对一堆按钮使用单个悬停脚本,并在鼠标悬停时使用变量?

java - 访问Java中多个类之间的变量

c# - 逐步应用更好的设计

apache - 没有 index.html 的 Tomcat 显示目录中的文件

java - 在 Android 上使用 Jmusic 混合音频

java - 创建 jar 时未调用属性文件

windows - 将 Apache 2.2 作为单个 httpd.exe 运行以进行调试