有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java IOException:未找到springxml

我刚接触Spring并编写了我的第一个代码。运行代码时出现以下错误:

Feb 09, 2014 1:21:23 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@64964f8e: startup date [Sun Feb 09 13:21:23 IST 2014]; root of context hierarchy
Feb 09, 2014 1:21:23 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring.xml] cannot be opened because it does not exist

我尝试了几种移动弹簧的组合。xml到不同的位置,我发现了相同的问题。 以下是我的文件夹结构: enter image description here


共 (2) 个答案

  1. # 1 楼答案

    1. 确保资源文件夹在您的classpth中
    2. spring.xml名称更改为/spring.xml。根据this example ClassPathXmlApplicationContext要求在路径之前使用斜杠。您应该将路径配置为绝对路径,而不是相对路径
  2. # 2 楼答案

    尝试将spring XML复制到com文件夹。它正在你当前的文件夹中搜索。如果要将其存储在其他文件夹中,那么它应该位于类路径上,并且在使用ClasspathXmlApplicationContext加载此文件时,应该使用项目根目录中的路径。如果资源文件夹不在类路径中,那么使用示例:/resources/spring。在您的案例中使用xml