初学Spring在用Resource rs=new ClassPathResource("applicationContext.xml");时老是遇到这个错误。后来发现用
ApplicationContext ctx=new  FileSystemXmlApplicationContext("WebContent/WEB-INF/applicationContext.xml");可以解决这个问题。
仔细研究了下:
之所以我用ClassPathResource中找不到applicationContext.xml是因为我的这个xml文件在建工程的时候默认放在了WebContent/WEB-INF/下面,但是用ClassPathResource是在当前目录也就是我这个java文件所在的目下进行寻找。因此把这个xml文件移动到我的src目录下面就ok了。
同理如果是用ApplicationContext ctx=new  ClassPathXmlApplicationContext()也是这样是从当前路径寻找,xml文件应在当前目录下。
但是如果用FileSystemXmlApplicationContext呢,它是根据指定的路径来进行寻找,所以要把路径写完整。现在xml路径在src文件夹下。那就要写:ApplicationContext ctx=new  FileSystemXmlApplicationContext("src/applicationContext.xml");
这是比较直接简单的写法,后来又查了查发现对于FileSystemXmlApplicationContext也可以采用:
1.加上classpath:前缀(这个时候xml要放在当前目录也就是src下)
ApplicationContext ctx=new  FileSystemXmlApplicationContext("classpath:applicationContext.xml");
2.加上file:把路径写全(用这个方法xml可以放任意地方)
ApplicationContext ctx=new  ClassPathXmlApplicationContext("ApplicationContext ctx=new  ClassPathXmlApplicationContext("file:F:/workspace/SpringExercis/src/applicationContext.xml");
综上,最简单的方法还是老老实实把xml放在src下方便。

package com.onlyfun;
/*
<p>Descption : </p>
@author: ChoviWu
@ version创建时间:2016年10月17日 下午5:58:56
@version : 1.0

*/

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class StaticFactoryMethodDemo {
public static void main(String[] args) {
// ApplicationContext context = new ClassPathXmlApplicationContext("bean-Musicconfig.xml");
// Resource rs = new ClassPathResource("bean-Musicconfig.xml");
// BeanFactory factory = new XmlBeanFactory(rs);
// IMusicBox music = (IMusicBox)factory.getBean("musicbox");
// IMusicBox musicbox =(IMusicBox) context.getBean("musicbox");
// musicbox.play();
}
}

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 id = "musicbox" class = "com.onlyfun.MusicBoxFactory"
factory-method= "MusicBoxFactory">
</bean>
</beans>

实现接口

package com.onlyfun;
/*
<p>Descption : </p>
@author: ChoviWu
@ version创建时间:2016年10月17日 下午5:53:16
@version : 1.0

*/
public interface IMusicBox {
public void play();
}

package com.onlyfun;
/*
<p>Descption : MusicBoxFactory </p>
@author: ChoviWu
@ version创建时间:2016年10月17日 下午5:52:38
@version : 1.0

*/
public class MusicBoxFactory {
public static IMusicBox MusicBoxFactory(){
return new IMusicBox(){
public void play(){
System.out.println("------音乐开始播放!!!-------");
}
};
}
}

class path resource [config.xml] cannot be opened because it does not exist的更多相关文章

  1. 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类:H ...

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

  3. mybatis异常解决:class path resource [SqlMapConfig.xml] cannot be opened because it does not exist

    解决方法: 缺失SqlMapConfig.xml文件.

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

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

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

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

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

  8. spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist

    spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path reso ...

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

随机推荐

  1. CentOS 5.8 x64 安装TomCat

    简单记录一下...虽然安装很简单... 首先下载配置安装 jdk http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u25-do ...

  2. Spring自学教程-ssh整合(六)

    以下是本人原创,如若转载和使用请注明转载地址.本博客信息切勿用于商业,可以个人使用,若喜欢我的博客,请关注我,谢谢!博客地址 感谢您支持我的博客,我的动力是您的支持和关注!如若转载和使用请注明转载地址 ...

  3. hadoop+海量数据面试题汇总(二)

    何谓海量数据处理? 所谓海量数据处理,无非就是基于海量数据上的存储.处理.操作.何谓海量,就是数据量太大,所以导致要么是无法在较短时间内迅速解决,要么是数据太大,导致无法一次性装入内存. 那解决办法呢 ...

  4. ZOJ 3927 Programming Ability Test

    水题,判断一下加起来是否大于等于80 #include<cstdio> #include<cstring> #include<cmath> #include< ...

  5. C++中string

    之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至 ...

  6. Runtime —— 从应用场景说起

    根据平时遇到的情况,通过查资料和自己的理解,对Runtime黑科技进行一次个人的学习总结

  7. 浅析IoC框架

    今日拜读了一篇关于IOC的文章,特意转载,和大家分享一下 1 IoC理论的背景    我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实 ...

  8. nodejs抓取数据二(列表解析)

    这里做得比较暴力,没有分页取出数据解析,O(∩_∩)O哈哈~,居然没有被挂机.不过解析的坑特别多...不过大部分我想要的数据都拿到了. //解析列表数据 var http = require(&quo ...

  9. EasyUI datagrid 的checkbox设置

    参考url: http://blog.csdn.net/baronyang/article/details/9323463 我的需求: 抓取数据生成的日志,日志中有部分是抓取失败的,需要将失败的发送到 ...

  10. 可用于Windows Server 2008 R2的Xbox One手柄、接收器驱动

    让客厅里的Gen8可以玩FC和PS1游戏,折腾了半天,终于将Xbox One手柄驱动弄好: http://www.drvsky.com/Microsoft/Xbox_One.htm http://ww ...