class path resource [config.xml] cannot be opened because it does not exist
初学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的更多相关文章
- 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 ...
- 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 ...
- mybatis异常解决:class path resource [SqlMapConfig.xml] cannot be opened because it does not exist
解决方法: 缺失SqlMapConfig.xml文件.
- 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 ...
- 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 [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里,就是放 ...
- 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 ...
- 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 ...
随机推荐
- SpringMVC轻松学习-注解的使用(三)
根据上一讲的例子,我们下面就注解的使用进行详细说明. 我们采用sprng MVC开发项目时,通常都会采用注解的方式,这样可以大大提高我们的开发效率.实现零配置.下面我们从零开始重新做一个spring ...
- Asp.net Ajax提供PageMethods调用
页面上的Javascript通过ASP.NET AJAX除了可以调用Web服务类,还可以调用网页中的静态方法. <%@ Page Language="C#" %> &l ...
- Codeforces#348DIV2/VK CUP 2016
昨天第一次开大小号打cf,发现原来小号提交之后大号在此提交同样的代码会被skipped掉,然后之后提交的代码都不记分,昨天a,b,c都是水题 A 题意:问一个物品最多能被分成多少份,分成的连续两份不能 ...
- 源码解析-knockout源码准备
准备包括心理和资源两方面. 心理 我看过一句话说,当你用一个框架时,不要忙着看一遍使用教程就开始写项目,先去看看框架原理. 这句话我深以为然.现今前端快速发展,很多前端攻城狮都很茫然:框架更新太快了, ...
- onchange、oninput、onpropertyChange事件的异同
onchange事件适用于input\textarea\select元素上,支持各大浏览器,该事件只有在键盘或者鼠标操作改变对象属性,且失去焦点时触发,脚本触发无效: oninput事件适用于inpu ...
- Python字符串的encode与decode研究心得——解决乱码问题
转~Python字符串的encode与decode研究心得——解决乱码问题 为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“/xe4/xb8/xad/xe6/x96/x8 ...
- STM32 USB 问题汇总(转)
源:STM32 USB 问题汇总 汇总1:STM32的USB例程修改步骤,来自http://blog.csdn.net/cy757/archive/2010/01/01/5117610.aspx 以下 ...
- IOS9中联系人对象的使用及增删改查操作的函数封装
之前克服重重困难把IOS9中新的类联系人搞明白了,现在把增删改查封装成了函数,如下: // // ViewController.m // IOS9中联系人CNContact的使用 // // Crea ...
- ABP架构解析
ABP总体介绍 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...
- HUSTOJ 2796 && SPOJ1811
传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2796 题解:后缀自动机,很裸,但是感觉对后缀自动机还不是特别理解,毕竟我太蒟蒻,等我精通 ...