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("------音乐开始播放!!!-------");
}
};
}
}
原文地址:https://www.cnblogs.com/ChoviWu/p/5970848.html