java.io.FileNotFoundException: D:\Workspaces\MyEclipse 8.5\spring\applicationContext.xml (系统找不到指定的文

来源:互联网 发布:editplus是什么软件 编辑:程序博客网 时间:2024/05/22 15:27

今天 重新写了一个spring项目发现出现这个问题:

 java.io.FileNotFoundException: D:\Workspaces\MyEclipse 8.5\spring\applicationContext.xml (系统找不到指定的文件。)

解决方案:

1

a:
import org.springframework.context.support.FileSystemXmlApplicationContext;
修改为
import org.springframework.context.support.ClassPathXmlApplicationContext;
b:
ApplicationContext context=new FileSystemXmlApplicationContext("applicationContext.xml");
修改为
ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");

2

spring配置文件放到classpath里,而不是WEB-INF下。

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:/applicationContext.xml");


究其原因:看spring api你会发现FileSystemXmlApplicationContextClassPathXmlApplicationContext,有不同我个人意见这就是导致不同结果的原因:

FileSystemXmlApplicationContext

Standalone XML application context, taking the context definition files from the file system or from URLs, interpreting plain paths as relative file system locations (e.g. "mydir/myfile.txt"). Useful for test harnesses as well as for standalone environments.

NOTE: Plain paths will always be interpreted as relative to the current VM working directory, even if they start with a slash. (This is consistent with the semantics in a Servlet container.)Use an explicit "file:" prefix to enforce an absolute file path.

ClassPathXmlApplicationContext

Standalone XML application context, taking the context definition filesfrom the class path, interpreting plain paths as class path resource names that include the package path (e.g. "mypackage/myresource.txt"). Useful for test harnesses as well as for application contexts embedded within JARs.

希望更明白的人加以指正


原创粉丝点击