Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't my regular application context load my properties file?

I'm trying to use the PropertyPlaceholderConfigurer in my application. My applicationContext-test.xml loads my properties file just fine, but my applicationContext.xml throws an exception. In both cases, I'm loading my properties file like this:

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

When I run my tests, it does not complain, but when I start my server, I get this exception:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/localdevelopment_Company.properties]
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:663)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:638)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
    at org.apache.catalina.core.StandardService.start(StandardService.java:519)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/localdevelopment_Company.properties]
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:117)
    at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
    at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:69)
    ... 21 more

I'm driving myself crazy trying to get this to work. Can someone help?

like image 368
Samo Avatar asked Nov 30 '10 20:11

Samo


1 Answers

This comes down to where different application contexts look for their resources.

A unit test context looks at the classpath, but a webapp context looks inside the webapp. So when your webapp looks for localdevelopment_Company.properties, it's looking for a file at the root of the webapp.

If your file is actually on the classpath, then you need to override the webapp context's default search location:

<value>classpath:localdevelopment_Company.properties</value>
like image 106
skaffman Avatar answered Nov 14 '22 21:11

skaffman