繁体   English   中英

java.io.FileNotFoundException:无法打开类路径资源[WEB-INF / classes / library.properties],因为它不存在

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

在我的Spring应用程序中,我有一个位于WEB-INF\\classes文件夹中的简单属性文件,因此它, DispatcherServlet和各种其他配置文件都在classpath

props文件在DispatcherServlet定义为:

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

propertiesFactory bean被注入到控制器中:

@Autowired 
private Properties propertiesFactory;

并在控制器的一种方法中使用:

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

但是如果整个应用程序都可以看到props文件,为什么不用这个程序呢?

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"));

并查看用作类路径条目的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM