2013-07-01 156 views
1

我遇到麻煩訪問與Spring的屬性文件。 這裏是我的樹形圖:春天沒有文件屬性文件

WEB-INF 
    - application-Context.xml 
    - properties 
     -applicationPerperties.xml 
     -csvHeader.properties 

這裏是我的導入在應用程序的context.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" 
    profile="dev, test, prod"> 

    <import resource="properties/applicationProperties.xml" /> 
</beans> 

這裏是applicationProperties.xml進口:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <util:properties id="csvDictionnaryProperties" location="csvHeader.properties" /> 
</beans> 

我不明白的是Spring找到applicationProperties.xml但沒有找到csvHeader.Properties。我試着將這個文件移動到根文件夾,添加類路徑前綴等,但我無法設法使這個工作。我總是得到FileNotFoundException

這裏是堆棧跟蹤:

11:24:54,710 ERROR [ContextLoader] Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'csvDictionnaryProperties': Invocation of init method failed; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/csvHeader.properties] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:608) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633) 
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:976) 
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1653) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:166) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:724) 
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/csvHeader.properties] 
    at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:140) 
    at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143) 
    at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98) 
    at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175) 
    at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156) 
    at org.springframework.beans.factory.config.PropertiesFactoryBean.createInstance(PropertiesFactoryBean.java:113) 
    at org.springframework.beans.factory.config.PropertiesFactoryBean.createProperties(PropertiesFactoryBean.java:98) 
    at org.springframework.beans.factory.config.PropertiesFactoryBean.afterPropertiesSet(PropertiesFactoryBean.java:69) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBe 

正如你所看到的,似乎它搜索csvHeader.properties /目錄中,但我不明白爲什麼。我對Spring很陌生,所以我不完全理解這一切。任何想法,爲什麼這不起作用?

回答

1

.properties文件需要位於CLASSPATH中。將其放入項目的WEB-INF/classes中,並將其稱爲classpath:csvHeader.properties。 Spring會發現它。

+0

它完美,謝謝!但我不明白爲什麼它沒有找到這些文件。 Spring是否聲明.properties文件必須位於類路徑中? – fxm

+1

它將它們視爲類路徑中的資源。是的,這是說。 – duffymo

0

將屬性文件放在類路徑的根目錄下,然後使用下面提供的配置。

在Eclipse中,source folder中的所有內容通常都在類路徑中,因此如果將該文件放在源目錄的根目錄下,Spring應該找到它。在Maven體系結構中,這個源文件夾通常是src/main/resources

<util:properties id="csvDictionnaryProperties" 
     location="classpath:csvHeader.properties" /> 
1

您可以嘗試以下方法。

<context:property-placeholder location="file:/WEB-INF/properties/applicationPerperties.xml" /> 

或者只是開始/ WEB-INF /屬性路徑/