Java, Spring, Unable to find /WEB-INF/spring.properties do I need to set it somewhere besides propertyConfigurer?

23,020

Solution 1

Looking at one of my webapps that uses a PropertyPlaceholderConfigurer, I see that I put the properties in /WEB-INF/classes and then configured the PPC to use it with a Spring classpath: URL; i.e.

    /WEB-INF/classes/substitution.properties

accessed as

    classpath:substitution.properties

Solution 2

Spring supports a ServletContextResource, which you can use by leaving off the resource prefix entirely. "You will get back a Resource type that is appropriate to that particular application context", and since we are using a web context, that resource will be a ServletContextResource.

The path is from the root of your webapp. Our paths look like

 <context:property-placeholder location="/WEB-INF/spring/appServlet/application.properties"/>

Solution 3

Your path ("src/main/webapp") suggests you are using Maven to build the project. If this is the case, put your .properties -file(s) to /src/main/resources and use "classpath:<filename>" to access them, everything under src/main/resources should be accessible through classpath without any further configuration.

Share:
23,020

Related videos on Youtube

Rick
Author by

Rick

Web programmer with an interest in web task automation, building websites, etc, I prefer to do everything in Python now as I have moved to it from using a variety of other languages in the past. I also like to do front-end AJAX / javascript work but am moving to do this through Python as well, with the Pyjamas framework.

Updated on October 16, 2020

Comments

  • Rick
    Rick over 3 years

    I am getting an error message that Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/spring.properties] cannot be opened because it does not exist. The spring.properties files does exist and is in my /WEB-INF directory (I have confirmed that it is in my build directory after building the project). I have it set on my project's .classpath directory like this:

    <classpathentry kind="src" path="src/main/webapp/WEB-INF/spring.properties"/>
    

    In my Spring application context, I have it entered like this:

    <bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="/WEB-INF/spring.properties" />
    </bean> 
    

    I apologize if this is a basic question but I really am perplexed as to what the problem is and how to solve it, I have done a lot of searching on this and can't seem to figure it out. Thanks for any advice

  • Rick
    Rick about 13 years
    thanks for the response, I did that and am now getting [ERROR] [main 11:00:33] (ContextLoader.java:initWebApplicationContext:220) Context initialization failed org.springframework.beans.factory.BeanInitializationExceptio‌​n: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/spring.properties] I am a bit perplexed by this

Related