简体   繁体   English

部署后从config.properties文件获取属性对象

[英]Getting Properties Object from config.properties file after deployment

I have a config file containing server information such as FTP URL's and their credentials. 我有一个配置文件,其中包含服务器信息,例如FTP URL及其凭据。 I am trying to, on deployment of my web app, reference the config.properties file to assign the stored values to local variables but for whatever reason cannot find the file. 我正在尝试在部署Web应用程序时参考config.properties文件,以将存储的值分配给局部变量,但是由于某种原因找不到该文件。

I have a getConfigProperties class: 我有一个getConfigProperties类:

public class getConfigProperties {

public Properties getConfig(String fileName) {
    // load config file
    Properties prop = new Properties();
    InputStream input = null;

    try {
        // grab config file from destination
        input = getConfigProperties.class.getClass().getResourceAsStream(
                fileName);
        // check if input is null
        if (input == null) {
            System.out.println("Sorry, unable to find "
                    + "config.properties");
            return null;
        }
        // load content
        prop.load(input);

        // start to declare variables
        // Prod vars
        System.out.println(prop.getProperty("prismUrlProd"));
        System.out.println(prop.getProperty("prismUserProd"));
        System.out.println(prop.getProperty("prismPassProd"));
        System.out.println(prop.getProperty("cardUrlProd"));
        System.out.println(prop.getProperty("cardUserProd"));
        System.out.println(prop.getProperty("cardPassProd"));
        System.out.println(prop.getProperty("pwcProd"));
        System.out.println(prop.getProperty("esdSignInProd"));
        System.out.println(prop.getProperty("emailProd"));

        // used to catch possible errors
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return prop;
}

public static void main(String[] args) {
    getConfigProperties a = new getConfigProperties();
    a.getConfig("/config/config.properties");
}
}

And I use this in another class to assign the variables. 我在另一个类中使用它来分配变量。 Inside my method, I set a Properties object to what is returned from getConfig(String filename): 在我的方法内部,我将一个Properties对象设置为从getConfig(String filename)返回的内容:

public static void initialize() {
    // load config file
    getConfigProperties config = new getConfigProperties();
    Properties prop = config.getConfig("/config/config.properties");
}

It's explained in properties file in web app that on deplyoment, our location of the config.properties changes, but when trying "/WEB-INF/classes/config/config.properties", I can't find the file. 在Web应用程序的属性文件中对此进行了解释,该文件在配置后会更改config.properties的位置,但是在尝试“ /WEB-INF/classes/config/config.properties”时,找不到该文件。 Using the main in my getConfigProperties class, I am able to find config.properties no problem and reference the text in my config file by printing it to console. 使用getConfigProperties类中的main,我可以毫无问题地找到config.properties并通过将其打印到控制台来引用配置文件中的文本。 Any possible suggestions as to where this file may be on deployment? 关于此文件可能在哪里部署的任何建议? Do I have to reference it a certain way? 我必须以某种方式引用它吗? Any help would much be appreciated. 任何帮助将不胜感激。

通过将config.properties放在我的src文件夹中并通过“ config.properties”对其进行引用,可以使它起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM