tomcat - 在 Grails 中动态配置文件路径

标签 tomcat grails war

我在以下位置有 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 中运行我的代码。

最佳答案

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

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

关于tomcat - 在 Grails 中动态配置文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39002803/

相关文章:

tomcat - Windows Server 2012 - Amazon ec2 Web 服务器无法使用公共(public) IP 从外部访问

debugging - 无法使用 intellij 13.1.2 在 grails 2.3.8 中设置断点

Grails - 从不同的数据源获取数据并将其保存在我的 Grails 数据库中

string - 错误WAR包装错误:字符串索引超出范围:11

tomcat - 在 Apache Tomcat 中记录 URL

authentication - 使用 Websphere Application Server (WAS) 和 LTPA 模拟用户/密码数据源

mysql - Mysql 连接器/J 8.0.17(com.mysql.cj.jdbc.Driver) 和 Tomcat 9 的问题

function - 相同参数的返回值不同

spring-boot - 将war文件多层打包成docker镜像

tomcat - 我怎样才能发布WAR文件