为什么常规应用程序上下文无法加载我的属性文件?

时间:2010-11-30 20:55:06

标签: java spring spring-mvc properties

我正在尝试在我的应用程序中使用PropertyPlaceholderConfigurer。我的applicationContext-test.xml很好地加载了我的属性文件,但是我的applicationContext.xml引发了异常。在这两种情况下,我都在加载我的属性文件:

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

当我运行我的测试时,它没有抱怨,但是当我启动服务器时,我得到了这个例外:

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

我正在疯狂地试图让它发挥作用。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:8)

这取决于不同的应用程序上下文查找其资源的位置。

单元测试上下文查看类路径,但webapp上下文查看webapp。因此,当您的网络应用程序查找localdevelopment_Company.properties时,它会在网络应用程序的根目录中查找文件。

如果您的文件实际上在类路径上,那么您需要覆盖webapp上下文的默认搜索位置:

<value>classpath:localdevelopment_Company.properties</value>