2014-07-06 115 views
4

在我的Spring應用程序中,我有一個位於文件夾WEB-INF\classes中的簡單屬性文件, DispatcherServlet和各種其他配置文件在classpathjava.io.FileNotFoundException:類路徑資源[WEB-INF/classes/library.properties]無法打開,因爲它不存在

<bean id="propertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
     <property name="location">    
      <value>/WEB-INF/classes/library.properties</value> 
     </property> 
    </bean> 

propertiesFactory豆被注入到一控制器:

@Autowired 
private Properties propertiesFactory; 

和In組成的控制器的方法作爲一種中:

道具文件在DispatcherServlet所限定

if (adminPassword.equals(propertiesFactory.getProperty("adminPassword"))) {   

這一切都完美的作品,除了一個測試專業克如下其中有一行:

ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("library-servlet.xml"); 

會拋出一個BeanCreationException

Injection of autowired dependencies failed 

因爲:

java.io.FileNotFoundException: class path resource [WEB-INF/classes/library.properties] cannot be opened because it does not exist 

但如果整個應用程序可以看到道具文件,何樂不爲一個程序?

+0

library-servlet.xml文件中有什麼... ??? –

+0

在\ WEB-INF \ classes中。 –

+0

我在問關於library-servlet.xml的內容 –

回答

6

WEB-INF/classes中的所有內容都添加到類路徑的根目錄中。因此,你需要參考您的資源僅僅作爲

library.properties 

或更好,但

classpath:library.properties 

<property name="location">    
    <value>classpath:library.properties</value> 
</property> 

你可能會發現運行

System.out.println(System.getProperty("java.class.path")); 

並查看用作類路徑的東西里斯。

+0

謝謝。這似乎工作。 –

相關問題