Spring 3.0 Could not load properties

31,870

Solution 1

Same issue i crossed,

Fix: Solution your bean like below:

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

create folder properties under WEB-INF it look like WEB-INF/properties/database.properties

because the class PropertyPlaceholderConfigurer default search properties folder first under /WEB-INF/ so it concatenate your file name as path

Solution 2

I think that you should change your code and put the properties like that :

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

Solution 3

Spring 4 this is how I would configure the properties file

@Configuration
@PropertySources({
        @PropertySource("classpath:application.properties"),
        @PropertySource(value = "${application.properties}", ignoreResourceNotFound = false)
})
public class WebServiceConfig {
---
}

And when starting my tomcat in setenv.sh I define the path as

-Dapplication.properties=file:location-of-file/application.properties"

Notice file: prefix here. That is important.

Now you can drop file anywhere on your file system and change the path without ever changing the code

Share:
31,870
cyber101
Author by

cyber101

Updated on April 20, 2020

Comments

  • cyber101
    cyber101 about 4 years

    Im the getting the following Error/exception when deploying web app:

    org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/WebAppProps]

    Below is the context tag that Im using in applicationContext.xml to point to WebAppsProps.properties file

       <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="classpath:/WEB-INF/WebAppProps.properties" />
    

    I have also used:

      <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/WebAppProps.properties" />
    

    The file is actualy in the filesystem & below is the snapshot of my project structure:

    enter image description here

    I also tried , putting "WebAppProps.properties" on the classpath and used these 2 variations

    variation 1:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
            <property name="location"> 
                <value>classpath:WebAppProps.properties</value> 
            </property> 
        </bean> 
    

    variation 2:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
            <property name="location"> 
                <value>WebAppProps.properties</value> 
            </property> 
        </bean> 
    

    Please see below: enter image description here

    However Im still getting same error/exception.

    Please Advise

    Thank you