【问题标题】:How to load properties file in Google App Engine?如何在 Google App Engine 中加载属性文件?
【发布时间】:2010-11-24 15:43:07
【问题描述】:

所以我正在尝试为我的项目添加一些功能,以允许在我的部署工件中使用用户定义的属性 - 一个简单的 key:value .properties 文件。我将 service.properties 文件放在

war/WEB-INF/my-service.properties 

在我的 ServiceImpl.java 构造函数中,我有以下内容:

String propertiesFileName = "my-service.properties"; 

URL propertyURL = ClassLoader.getSystemResource(propertiesFileName);
URL propertyURL2 = this.getClass().getClassLoader().getResource(propertiesFileName);
URL propertyURL3 = this.getClass().getClassLoader().getResource( "WEB-INF/" + propertiesFileName);
URL propertyURL6 = this.getClass().getClassLoader().getResource(
       "E:/Projects/eclipse-workspace/projectName/war/WEB-INF/" + propertiesFileName);

属性 URL 的所有实例均为空。我知道我遗漏了一些绝对明显的东西,但我需要第二双眼睛。问候。

编辑:

啊,我似乎很困惑,因为默认的 GAE 项目在 /war 中创建了一个 logging.properties 文件。来自Google App Engine documentation:

App Engine Java SDK 包含一个模板 logging.properties 文件,位于 appengine-java-sdk/config/user/ 目录中。要使用它,请将文件复制到您的 WEB-INF/classes 目录(或 WAR 中的其他位置),然后将系统属性 java.util.logging.config.file 复制到“WEB-INF/classes/logging.properties”(或无论您选择哪个路径,都相对于应用程序根目录)。您可以在 appengine-web.xml 文件中设置系统属性,如下所示:

【问题讨论】:

  • 我认为这是您的问题而不是您的应用程序中的拼写错误,但您声明您的文件名为“service.properties”但 propertiesFileName 设置为“my-service.properties”!跨度>
  • @Todd:是的,这是一个错字,我会改正的。

标签: java google-app-engine jakarta-ee


【解决方案1】:

正如Mike 在他对jsights 答案的评论中提到的那样,如果我使用它对我有用

this.getClass().getClassLoader().getResourceAsStream("filename.properties");

(删除第一个斜线)将文件放入 WEB-INF/classes 后。

【讨论】:

    【解决方案2】:

    我认为你需要的是这样的:

    String filePath = servletContext.getRealPath("/WEB-INF/views/") + "/" + mav.getViewName() + ".vm"; FileInputStream in = new FileInputStream(filePath);

    我从 spring 中获得了 servletContext:@Autowire ServletContext。

    【讨论】:

      【解决方案3】:

      尝试将 service.properties 放在 WEB-INF/classes 中。然后它应该可以通过 :

      访问
      this.getClass().getClassLoader().getResourceAsStream("/filename.properties");
      

      【讨论】:

      • 你知道为什么我需要删除路径开头的“/”才能让它工作吗?
      • 我收到了 NPE。我正在尝试从我的班级 (kotlin/ClassName) val credentials = GoogleCredentials.fromStream(this.javaClass.getClassLoader().getResourceAsStream("/coinverse-media-staging-firebase-config.json")) 访问文件 (kotlin/webapp/WEB-INF/fileName.json)。
      猜你喜欢
      • 2013-12-17
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多