【问题标题】:java.io.FileNotFoundException: class path resource [WEB-INF/classes/library.properties] cannot be opened because it does not existjava.io.FileNotFoundException:类路径资源 [WEB-INF/classes/library.properties] 无法打开,因为它不存在
【发布时间】:2014-08-27 04:48:42
【问题描述】:

在我的 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文件,那为什么不是这个程序呢?

【问题讨论】:

  • library-servlet.xml 文件中有什么...???
  • 我在询问 library-servlet.xml 的内容
  • 与任何其他 DispatcherServlet 非常相似,即 JavaBean 注册以及所有 Spring 和 Hibernate 配置数据。你想知道具体的事情吗?

标签: java spring dependency-injection properties-file


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 2019-09-05
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多