Spring报错:java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
感谢:http://blog.chinaunix.net/uid-20681545-id-184633.html提供的解决方案,非常棒 !
问题说明:
新建一个Spring项目,新建一个Bean类:HelloWorld类,Main.java是主程序,xml是Spring配置文件。
项目结构如下:
打开文件夹,src目录下的结构如下:
打开bin文件夹,目录如下
HelloWorld.java代码:
package com.tt.spring.beans; public class HelloWorld { private String name; public void setName(String name) {
this.name = name;
} public void hello(){
System.out.println("hello: "+name);
} }
Main.java
package com.tt.spring.beans; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args){
/*
//创建HelloWorld的一个对象
HelloWorld helloWorld = new HelloWorld();
//为name属性赋值
helloWorld.setName("tt");
*/
//调用hello方法 //1.创建Spring 的 IOC 容器对象
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.从IOC容器中获取Bean实例
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld"); //3.调用Hello方法
helloWorld.hello(); }
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置bean -->
<bean id="helloWorld" class="com.tt.spring.beans.HelloWorld">
<property name="name" value="Spring"></property>
</bean>
</beans>
运行Main.java程序,结果显示报错。报错如下:
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
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:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.tt.spring.beans.Main.main(Main.java:18)
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:158)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
... 13 more
问题分析:

现在目录树如下:
1 package com.tt.spring.beans;
2
3 public class Car {
4
5 private String brand;
6 private String corp;
7 private int price;
8 private int maxSpeed;
9
10
11 public Car() {
12 System.out.println("Car's Constructor");
13 }
14
15 public Car(String brand, String corp, int price) {
16 super();
17 this.brand = brand;
18 this.corp = corp;
19 this.price = price;
20 }
21
22 @Override
23 public String toString() {
24 return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price + ", maxSpeed=" + maxSpeed + "]";
25 }
26
27 }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <context:component-scan base-package="com.tt.spring.beans"></context:component-scan>
<!--
配置bean
class:bean的全类名,通过反射的方式在IOC容器中创建Bean实例,所以要求Bean中必须有无参的构造器
id:标识容器中的bean,id唯一
-->
<bean id="helloWorld" class="com.tt.spring.beans.HelloWorld">
<property name="name" value="Spring"></property>
</bean> <!-- 通过构造方法来配置bean的属性 -->
<bean id="car" class="com.tt.spring.beans.Car">
<constructor-arg value="Audi"></constructor-arg>
<constructor-arg value="chengdu"></constructor-arg>
<constructor-arg value="30000"></constructor-arg>
</bean> </beans>
Main.java
控制台打印信息如下:
看到没?只能获取一个bean实例,不能获取两个。
原因分析:
在项目里,applicationContext.xml是在src最里面一层的,不在src的那一层,所以一旦运行Main.java文件,相应的就会在bin的目录下生成对应的applicationContext.xml文件。
但是我们又在bin文件夹里做过粘贴applicationContext.xml。
所以说applicationContext.xml的目录压根就是混乱的!!!既是处于根目录,又处于com最里面的目录。
要么在项目上将xml移到src那一层,那么bin底下的xml也对应着-------xml就处于根目录。
要么在项目上仍然位于com的最里面,那么bin那里就不要将xml移出来----xml就处于最里面的目录
至于xml文件究竟在哪个目录,就该用相应的读取方式。
ClassPathXmlApplicationContext和FileSystemXmlApplicationContext读取xml
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml")

Spring报错:java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist的更多相关文章
- nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may ...
- Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be ope
1.错误描述 java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.tes ...
- parsing XML document from class path resource [applicationtext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationtext.xml] cannot be opened because it does not e
控制台异常: parsing XML document from class path resource [applicationtext.xml]; nested exception is java ...
- nested exception is java.io.FileNotFoundException: class path resource [jdbc.properties] cannot be opened because it does not exist
Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [j ...
- java.io.FileNotFoundException class path resource [xxx.xml] cannot be opened
没有找到xxx.xml,首先确定你项目里有这个文件吗,如果没有请添加,或者你已经存在配置文件,只是名字不是xxx.xml,请改正名字.此外还要注意最好把xxx.xml加入到classpath里,就是放 ...
- class path resource [applicationContext.xml] cannot be opened because it does not exis
使用maven创建web工程,将spring配置文件applicationContext.xml放在src/resource下,用eclipse编译时提示class path resource [ap ...
- nested exception is java.io.FileNotFoundException: class path resource [spring/spring-datasource-mog
spring单元測试时发现的问题: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsin ...
- 关于SpringMVC项目报错:java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/xxxx.xml]
关于SpringMVC项目报错:java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/xxxx ...
- Caused by: java.io.FileNotFoundException: class path resource [spring/springmvc.xml] cannot be opene
Caused by: java.io.FileNotFoundException: class path resource [spring/springmvc. ...
随机推荐
- [.net基础]访问修饰符
标题:[.net基础]访问修饰符 一.前言 基础掌握不牢固啊,所以记录下来. 二.方法访问修饰符Internal (1).创建工程ParentAndSon (2).添加类ModelA namespac ...
- 20145302张薇《Java程序设计》第九周学习总结
20145302 <Java程序设计>第九周学习总结 教材学习内容总结 第十六周 JDBC简介 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JDBC目的:让Jav ...
- 20145307第四次JAVA学习实验报告
20145307实验四 Android开发基础 实验内容 基于Android Studio开发简单的 Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事 ...
- 学Git,用Git ②
之前介绍了git的最核心功能游戏存档式的本地版本管理.这会我们介绍git剩下的两个核心功能:分支和远程仓库. 1.Git游戏存档进化版--Git分支 git分支的思想很有意思,git允许我们可以随时从 ...
- 如何生成SSH key
SSH key提供了一种与GitHub通信的方式,通过这种方式,能够在不输入密码的情况下,将GitHub作为自己的remote端服务器,进行版本控制 步骤 检查SSH keys是否存在 生成新的ssh ...
- js 的胖箭头问题
我们在声明函数的时候通常是 var foo function(a){ console.log(a) }; 用ES6 我们写成了这样 var foo = a =>{ console.log(a); ...
- html-常用块级及行级标签
1.常见块级标签 <h1></h1>......<h6></h6>:标题标签 h标签:标题标签,自动加粗,h1最大,h6最小 例:(前后隔一行) ...
- webservice用cxf发布SOAP
cxf的安装,就是把文件解压,然后配置环境变量 http://cxf.apache.org/download.html这是官网下载 解压到这里 环境变量 wsdl2java命令测试 1.新建java项 ...
- C#代码实现 Excel表格与Object互相转换,Excel表格导入数据库(.NET2.0 .NET4.0)
前些天在工作上遇到这个需求,在GitHub找到一个开源代码可以用,Fork了一个版本,整理一下发出来. ①.Net项目中使用Nuget安装一个 NPOI 包 https://github.com ...
- PHP 重载方法 __call()
__call() 方法用于监视错误的方法调用. __call()(Method overloading) 为了避免当调用的方法不存在时产生错误,可以使用 __call() 方法来避免.该方法在调用的方 ...