How to load properties file in Google App Engine?

2020-02-28 03:08发布

So I'm trying to add some ability to my project to allow user-defined properties in my deployment artifact - a simple key:value .properties file. I place the service.properties file in

war/WEB-INF/my-service.properties 

And in my ServiceImpl.java constructor I have the following:

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);

All instances of Property URL are null. I know I'm missing something absolutely obvious, but I need a second pair of eyes. Regards.

EDIT:

Ah, it seems I was confused as the default GAE project creates a logging.properties file in /war. From the Google App Engine documentation:

The App Engine Java SDK includes a template logging.properties file, in the appengine-java-sdk/config/user/ directory. To use it, copy the file to your WEB-INF/classes directory (or elsewhere in the WAR), then the system property java.util.logging.config.file to "WEB-INF/classes/logging.properties" (or whichever path you choose, relative to the application root). You can set system properties in the appengine-web.xml file, as follows:

3条回答
疯言疯语
2楼-- · 2020-02-28 03:50

Try putting the service.properties in WEB-INF/classes. Then it should be accessible just with :

this.getClass().getClassLoader().getResourceAsStream("/filename.properties");
查看更多
祖国的老花朵
3楼-- · 2020-02-28 03:52

I think what you will need is something like this:

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

I get the servletContext from spring: @Autowire ServletContext.

查看更多
▲ chillily
4楼-- · 2020-02-28 04:02

As Mike mentioned in his comment to jsights answer, it worked for me if I used

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

(removed the first slash) after placing the file in WEB-INF/classes.

查看更多
登录 后发表回答