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

摩根先生

在我的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>

propertiesFactorybean被注入到一个控制器:

@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文件,那么为什么不使用该程序呢?

Sotirios Delimanolis

所有内容都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"));

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章