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 ...
随机推荐
- 检测网站挂马程序(Python)
系统管理员通常从svn/git中检索代码,部署站点后通常首先会生成该站点所有文件的MD5值,如果上线后网站页面内容被篡改(如挂马)等,可以比对之前生成MD5值快速查找去那些文件被更改,为了使系统管理员 ...
- Arduino中hex文件的保存及应用(转)
源:Arduino中hex文件的保存及应用 arduino在编译.链接.下载之后,hex文件自动删除了,造成软件仿真(如用proteus仿真)及其他单片机板应用的不便.以下是自己实践的小结,与大家分享 ...
- 3D VR卡镜的使用方法
先把它展开 然后把它卡在手机中间 介绍一个VR游戏资源 Chair In a Room,这是一个立体沉浸式的3D游戏,原理是陀螺仪传感器随着手机转动可以观察整个三维房间 如图所示,点击进入,将两眼放到 ...
- Tomcat热部署:Maven项目一键部署到Tomcat服务器 - 支持多环境
参考:Eclipse中的Maven项目一键部署到Tomcat服务器 - 支持多环境部署 命令 debug模式设置关联源码 eclipse --> 项目右键 --> Debug As --& ...
- mysql 千万量级的表的优化
参考: 一 大的优化方向: 数据结构优化,慢查询优化,索引优化,mysql参数设置优化 数据结构优化:先读写分离.再垂直拆分.再水平拆分! 说3点 1. 设计合适的索引,基于主键的查找,上亿数据也是 ...
- delphi 中COPY()函数的意思
Trim(copy(m,11,5)):copy里面的3个参数(m,11,5)分别是什么意思?COPY还有其他的参数吗? m :就是copy源,就是一个字符串,表示你将要从m里copy一些东西11 : ...
- S3C2440时钟系统详解
在讲述系统时钟之前,因为这些设备都是挂靠在系统时钟上的,所以必须先说系统时钟,S3C2440的时钟系统如下 外部时钟源分两种,晶振或者外部频率,由om3-2选择,时钟电路根据两种选择也有两种 我们来分 ...
- php json_decode返回失败的排查
参考网址: https://segmentfault.com/a/1190000006154011 返回失败时,通过 json_last_error可以获取其json解析失败的原因.
- lua 函数
1.函数只有一个参数,且该参数为table 或 字符串时,调用函数可以省略() print"hello world" 同 print("hello world" ...
- iOS真机调试配置
啊!生活不易啊~~~~据说这个过程都可以当做简历技能了... 准备:已经具备了企业开发者账号,和相关证书 目标:让爪机可以真机调试 过程:1.登录官方开发网站 https://developer.ap ...