0

如何为同一项目中的用户进行不同的项目设置。我有一个项目,其中包含一些诸如 email.properties 之类的 .properties 文件,其中包含特定于用户的设置。我需要诸如特定于用户的环境设置之类的东西,例如:email..properties,并且变量包含在操作系统环境中,或者可能在项目文件

4

2 回答 2

0

结果做同样的事情:首先我在项目根目录中创建了属性文件夹,在那里添加包含当前环境名称的文件 env.properties(或者用户可以将其添加为 JVM 启动参数。我添加了接收属性文件的-Denv=<env name>静态类和方法Properties getProperty(String fileName)名称作为参数,并将文件中的所有记录作为 java.util.Properties 返回。

public static Properties loadProperties(String fileName)
{
    Properties properties = null;
    if (propertiesMap.containsKey(fileName)) {
        properties = (Properties)properties.get(fileName);
    } else {
        String environment = getEnvironment();
        try {
            properties = (new PropertiesLoaderImpl()).LoadAllPropertiesForUtilAndEnv(environment, fileName);
        } catch (FileNotFoundException exception) {}            
    }
    return properties;
}

private static String getEnvironment() {
    // Проверка на наличие параметра при запуске java-машины
    String environment = System.getProperty("env");
    if (environment == null) {
        try {
            // Попытка найти файл env.properties
            ResourceBundle bundle = ResourceBundle.getBundle("properties/env");
            environment = bundle.getString("env");
        } catch (MissingResourceException exception) {
            environment = "";
        }
    }
    return environment;
}

属性搜索以这种方式实现: 1. 查看 ..proeprties;2.如果没有合适的属性,则在.property中查找;3.否则返回empy。

于 2012-12-26T10:20:35.977 回答
0

以下是我在一些项目中的做法。我为属性文件的路径创建了一个系统属性。该路径在项目之外,因此永远不会提交属性文件。但是为了帮助第一次运行该项目的其他人,我提交了一个带有默认选项的模板属性文件。

在 intellij 中,我使用与同事不同的 -D 选项启动项目,并且启动选项没有被提交(因为我们没有提交 .idea 文件夹)。

于 2012-12-18T22:21:03.737 回答