gre*_*reg 5 java amazon-web-services aws-lambda
我尝试将属性从文件加载到 Properties 类中。我希望这个解决方案能够工作: How to load property file from classpath in AWS lambda java
\n\n我有一个带有很少静态方法的类,我想将其用作配置持有者。里面有这么一行
\n\nfinal InputStream inputStream = \nConfig.class.getClass().getResourceAsStream("/application-main.properties");\nRun Code Online (Sandbox Code Playgroud)\n\n并且它总是返回 null。我下载了 Lambda 使用的 zip 包,该文件位于根目录中。尽管如此,还是不行。
\n\n有人有类似的问题吗?
\n\n编辑:\n配置文件在这里:
\n\nproject\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80src\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80main\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80resources\n\xe2\x94\x82 application-main.properties\nRun Code Online (Sandbox Code Playgroud)\n\n编辑:\n我的“临时”解决方法如下所示:
\n\n// LOAD PROPS FROM CLASSPATH...\ntry (InputStream is = Config.class.getResourceAsStream(fileName)) {\n PROPS.load(is);\n } catch (IOException|NullPointerException exc) {\n // ...OR FROM FILESYSTEM\n File file = new File(fileName);\n try (InputStream is = new FileInputStream(file)) {\n PROPS.load(is);\n } catch (IOException exc2) {\n throw new RuntimeException("Could not read properties file.");\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n在测试期间,它从类路径读取,在 AWS Lambda 运行时部署后,它使用文件系统。为了识别文件,我使用了 env 变量:
\n\nfileName = System.getenv("LAMBDA_TASK_ROOT") + "/application-main.properties";\nRun Code Online (Sandbox Code Playgroud)\n\n但我宁愿只使用类路径而不解决这个问题。
\n假设您有以下文件夹结构:
\n\nproject\r\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80src\r\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80main\r\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80resources\r\n\xe2\x94\x82 config.propertiesRun Code Online (Sandbox Code Playgroud)\r\n还有你的 Lambda 处理程序:
\n\nClassLoader loader = Config.class.getClassLoader();\r\nInputStream input = loader.getResourceAsStream("config.properties");Run Code Online (Sandbox Code Playgroud)\r\n这可能有效...
\n| 归档时间: |
|
| 查看次数: |
11044 次 |
| 最近记录: |