2010-11-30 63 views
7

我想在我的應用程序中使用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 

我開始瘋狂地試圖讓這個工作。有人可以幫忙嗎?

回答

8

這歸結爲不同的應用程序上下文查找其資源的位置。

單元測試上下文查看類路徑,但webapp上下文在webapp中查找。所以當你的web應用程序尋找localdevelopment_Company.properties時,它正在web應用程序的根目錄尋找一個文件。

如果你的文件實際上是在類路徑中,那麼你需要重寫Web應用程序上下文的默認搜索位置:

<value>classpath:localdevelopment_Company.properties</value> 
相關問題