【问题标题】:Configuring file path dynamically in Grails在 Grails 中动态配置文件路径
【发布时间】:2016-08-17 17:21:43
【问题描述】:

我在以下位置有 database.properties 文件: grails-app/conf/database.properties

现在我需要读取文件并将其作为属性加载如下:

public static Properties loadProperties(String fileName) {          
    Properties prop = new Properties();                             
    try {                                                           
        FileInputStream inputStream = new FileInputStream(fileName);
        prop.load(inputStream);                                     
    } catch (IOException e) {                                       
        e.printStackTrace();                                        
    }                                                               

    return prop;                                                    
}
  • 当我将文件名指定为 database.properties 时,它 显示错误为:java.io.FileNotFoundException: database.properties 在我们跑步的时候。

  • 另一方面,当我将文件名指定为 grails-app/conf/database.properties 它将在本地运行良好。但 当我在 tomcat 服务器中作为 WAR 文件运行时,它将显示与上面相同 错误:java.io.FileNotFoundException:database.properties。

现在我该如何解决上述问题,并且可以在本地和 tomcat 中运行我的代码。

【问题讨论】:

标签: tomcat grails war


【解决方案1】:

您需要重新访问http://docs.grails.org/latest/guide/conf.html#dataSource 上的 Grails 数据源文档。

没有理由将文件存储在与 Grails 配置相同的目录中,并在您可以在数据源配置中包含所需的属性时尝试显式读取它。你会很高兴你没有把所有的精力都花在“重新发明轮子”上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 2011-04-17
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2019-08-08
    相关资源
    最近更新 更多