Tomcat 在Grails中动态配置文件路径

Tomcat 在Grails中动态配置文件路径,tomcat,grails,war,Tomcat,Grails,War,我在以下位置有database.properties文件: grails app/conf/database.properties 现在我需要读取文件并将其作为属性加载,如下所示: public static Properties loadProperties(String fileName) { Properties prop = new Properties(); try {

我在以下位置有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它将在本地正常运行。但是 当我作为WAR文件在tomcat服务器中运行时,它将显示与上面相同的内容 错误:java.io.FileNotFoundException:database.properties


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

您需要重新访问位于的Grails数据源文档


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

这不是Grails实现外部化配置的方式。阅读文档: