感谢: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

问题分析:

个人的文件目录树大概这样:
 
SpringDemo根目录
   --src java源文件
   --WEB-INF
        --web.xml
        --applicationContext.xml
        --classes文件夹
           --com包
              --java编译后的class文件
   --index.jsp
根据网上查找的解决办法,我把applicationContext.xml从bin的com层的最里面复制出来放到classess文件夹即bin文件夹中(不能放到com中,应为xml中class路径),该问题解决了。

现在目录树如下:

   --src java源文件
   --WEB-INF
         --web.xml
         --classes文件夹
             --applicationContext.xml
             --com包
                  --java编译后的class文件
   --index.jsp
 
但是,当新建另一个Bean:Car类时,问题马上来了!!!!!!!
  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 FileSystemXmlApplicationContext("WEB-INF/classes/applicationContext.xml");

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml")

 
 现在这个项目是使用ClassPathXmlApplicationContext读取xml文件的,就把配置文件appicationContext.xml放到src的那一层。

如果使用FileSystemXmlApplicationContext读取,则需把配置文件放到整个项目的根目录。
 
 

Spring报错:java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. java.io.FileNotFoundException class path resource [xxx.xml] cannot be opened

    没有找到xxx.xml,首先确定你项目里有这个文件吗,如果没有请添加,或者你已经存在配置文件,只是名字不是xxx.xml,请改正名字.此外还要注意最好把xxx.xml加入到classpath里,就是放 ...

  6. 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 ...

  7. nested exception is java.io.FileNotFoundException: class path resource [spring/spring-datasource-mog

    spring单元測试时发现的问题: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsin ...

  8. 关于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 ...

  9. Caused by: java.io.FileNotFoundException: class path resource [spring/springmvc.xml] cannot be opene

                        Caused by: java.io.FileNotFoundException: class path resource [spring/springmvc. ...

随机推荐

  1. ABP官方文档翻译 1.3 模块系统

    ABP模块系统 介绍 模块定义 生命周期方法 PreInitialize Initialize PostInitialize Shutdown 模块依赖 插件系统 ASP.NET Core ASP.N ...

  2. oracle 分区表详解

    一.分区表的概述: Oracle的表分区功能通过改善可管理性.性能和可用性,从而为各式应用程序带来了极大的好处.通常,分区可以使某些查询以及维护操作的性能大大提高.此外,分区还可以极大简化常见的管理任 ...

  3. oracle过程书写规范

    ORACLE存储过程编码规范 1.1         变量规范 Ø        变量名全部采用小写,局部变量名使用“v_”开头,输入参数以“i_开头,输出参数以“o_”开头,输入输出参数用io_开头 ...

  4. 对于JVM中方法区,永久代,元空间以及字符串常量池的迁移和string.intern方法

    在Java虚拟机(以下简称JVM)中,类包含其对应的元数据,比如类的层级信息,方法数据和方法信息(如字节码,栈和变量大小),运行时常量池,已确定的符号引用和虚方法表. 在过去(当自定义类加载器使用不普 ...

  5. ThreadLocal的设计理念与作用

    转自:http://www.iteye.com/topic/103804 首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线 ...

  6. WCF可靠性会话之服务分流

    可靠性绘画答条件: (1)服务绑定支持可靠性绘画:特别是netTcpBinding.wsHttpBinding及wsDualHttpBinding. wsDualHtttpBinding可靠性绘画始终 ...

  7. [Linux 002]——Linux的常用命令

    经过前面的学习,大概了解了计算机组成原理和操作系统的一些知识.尽管这些知识都是琐碎的,拼凑的,在以后的工作和学习中仍需进行深入的了解.但是这些预备知识对于准备跨入 Linux 大门的童鞋来说,应该是足 ...

  8. SSH免密码登录Linux

    如果两台linux之间交互频繁,但是每次交互如果都需要输入密码,就会很麻烦,通过配置SSH就可以解决这一问题 下面就说下配置流程(下面流程在不同机器上全部操作一边) 1)cd ~到这个目录中 2)ss ...

  9. 第十篇:Spark SQL 源码分析之 In-Memory Columnar Storage源码分析之 query

    /** Spark SQL源码分析系列文章*/ 前面讲到了Spark SQL In-Memory Columnar Storage的存储结构是基于列存储的. 那么基于以上存储结构,我们查询cache在 ...

  10. geoserver源码学习与扩展——跨域访问配置

    在 geoserver源码学习与扩展——restAPI访问 博客中提到了geoserver的跨域参数设置,本文详细讲一下geoserver的跨域访问配置. geoserver的跨域访问依赖java-p ...