12
votes

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
I assume it is a typo in your question rather than your app, but you state that your file is called "service.properties" yet propertiesFileName is set to "my-service.properties"!Todd Owen
@Todd: Yes, it's a typo, I'll correct it.Chris K

3 Answers

10
votes

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

this.getClass().getClassLoader().getResourceAsStream("/filename.properties");
1
votes

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.

0
votes

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.