class path resource [config.xml] cannot be opened because it does not exist

Posted ChoviWu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

单点登录-----遇到问题-----FileNotFoundException: class path resource-UsernamePasswordWrapperAuthenticatio(代码

异常IOException parsing XML document from class path resource [xxx.xml]

Error creating bean with name ‘redisConnectionFactory‘ defined in class path resource...

Error creating bean with name ‘redisConnectionFactory‘ defined in class path resource...

SpringBoot Error creating bean with name 'dataSource' defined in class path resource。。。

报错 Error creating bean with name ‘elasticsearchTemplate‘ defined in class path resource