java - Spring:调用 new ClassPathXmlApplicationContext() 时出现 FileNotFoundException

标签 java spring

我遇到了主题中描述的问题。 我打印了工作目录以确定我在哪里运行。 我尝试过以下代码:

   public static void main(String[] args) {

        System.out.println("Working Directory = " +
                System.getProperty("user.dir"));

        //load the spring configuration file
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        ...
}

并将 xml 放入项目根目录和目标目录中,用于上面的第一次和第二次尝试,并将 xml 放入 src 目录中,用于第二次和第三次尝试(即使我不认为这是正确的位置,但所有目前留给我的是向任何方向拍摄)。

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:126)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:92)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:465)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:395)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at ttt.springdemo.HelloSpringApp.main(HelloSpringApp.java:14)
Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    ... 13 more

Project Structure

这是manifest.mf 文件:

Manifest-Version: 1.0
Built-By: TalT
Class-Path: lib/spring-core-5.0.8.RELEASE.jar lib/spring-jcl-5.0.8.REL
 EASE.jar lib/spring-context-3.0.2.RELEASE.jar lib/spring-aop-3.0.2.RE
 LEASE.jar lib/aopalliance-1.0.jar lib/spring-beans-3.0.2.RELEASE.jar 
 lib/spring-expression-3.0.2.RELEASE.jar lib/spring-asm-3.0.2.RELEASE.
 jar
Created-By: Apache Maven 3.5.4
Build-Jdk: 1.8.0_171
Main-Class: ttt.springdemo.HelloSpringApp

请指教。 谢谢。

最佳答案

错误非常明显:找不到文件applicationContext.xml。这意味着它不存在或不在正确的位置。

可能性:

  • 该文件不存在
  • 您使用的是区分大小写的文件系统(通常在 linux/unix/macOS 上),并且您没有在文件和代码中使用完全相同的文件名
  • 文件位置不正确。您使用 ClassPath 方法。该文件必须位于 classPath 中。例如,不是工作目录。这不是同一件事。
    • 如果您使用 java -jar your.jar 启动应用程序,则该文件需要位于 your.jar 文件的根目录中
    • 如果使用 java -cp a/directory my.Application 启动,该文件必须位于 a/directory 目录中。
    • 如果某些资源文件位于 random/directory 中,您需要使用 java -cp randaom/directory:...other_classpath... your.Application ... 启动应用程序。并不是说random/direction 可以是.(如果它与工作目录相同)。

关于java - Spring:调用 new ClassPathXmlApplicationContext() 时出现 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52026472/

相关文章:

java - 在android上实现频闪(闪光灯闪烁)效果,以相同的频率闪烁

java - 我想将 excel 转换为 xml

java - 类型 ClassName 的 getId() 方法未定义

spring - 使用 JSF 作为 Spring MVC 的 View 技术

spring - Spring Boot框架需要Spring安全管理控制台

java - 是否可以控制 Mixer.Info 字符集?

java - 在查询的任何表中都找不到列 (XIT115)(或 SLV 未定义)

Java进程 "The pipe has been ended"问题

java - 为什么Netty需要线程池?

java - 完成我的第一个 Spring MVC 应用程序 - 老板想看我的单元测试 - 我从哪里开始?