如何从AWS Lambda Java中的类路径加载属性文件 [英] How to load property file from classpath in AWS lambda java
问题描述
我编写了AWS lambda函数,因为我想从属性文件中读取数据库连接详细信息,并在我的类路径中读取它,但是我无法加载该文件.这是我的代码:
I have written AWS lambda function in that i want to read database connection details from property file and which in my classpath, but I am not able to load that file.Here is my code:
InputStream input = DBConfiguartion.class.getResourceAsStream("appsettings");
Reader r = new InputStreamReader(input, "UTF-8");
Properties prop = new Properties();
prop.load(r);
如果我当时通过正常的Java控制台应用程序运行此代码,则该代码正常工作,但是每当我将其作为AWS lambda函数运行时, InputStream 为空.
If I run this code through normal java console application that time it is working, but whenever i run it as AWS lambda function then InputStream is coming null.
推荐答案
您只有一个字符.这是一个工作示例,我必须做同样的事情:
You are only one character off. Here's a working example that I have to do the same thing:
InputStream is = DBConfiguartion.class.getResourceAsStream("/lambda.properties");
Properties properties = new Properties();
properties.load(is);
在构建部署jar时,它可与以下maven文件结构一起使用:
This works with the following maven file structure when building the deployment jar:
- 项目
- project/src/main/java
- project/src/main/java/com/something/DBConfiguartion.java-
- project/src/main/resources
- project/src/main/resources/lambda.properties
这篇关于如何从AWS Lambda Java中的类路径加载属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!