[英]Context doesn't exist
I have very strange problem with spring context. 春天语境我有一个非常奇怪的问题。
public static void main(String[] args) {
File file = new File("/home/user/IdeaProjects/Refactor/src/spring-cfg.xml");
System.out.println("Exist "+file.exists());
System.out.println("Path "+file.getAbsoluteFile());
ApplicationContext context = new ClassPathXmlApplicationContext(file.getAbsolutePath());
Show on console: 在控制台上显示:
Exist true
Path /home/user/IdeaProjects/Refactor/src/spring-cfg.xml
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [home/user/IdeaProjects/Refactor/src/spring-cfg.xml]; nested exception is java.io.FileNotFoundException: class path resource [home/user/IdeaProjects/Refactor/src/spring-cfg.xml] cannot be opened because it does not exist
You're trying to load it as if /home/user/IdeaProjects/Refactor/src/spring-cfg.xml
is a resource on the classpath - it's not, it's just a regular file. 您正在尝试加载它,好像
/home/user/IdeaProjects/Refactor/src/spring-cfg.xml
是类路径上的资源 - 它不是,它只是一个常规文件。 Try using FileSystemXmlApplicationContext
instead... or specify a genuine classpath resource, eg just spring-cfg.xml
assuming that your src
directory is in your classpath. 尝试使用
FileSystemXmlApplicationContext
代替...或指定一个真正的类路径资源,例如spring-cfg.xml
假设你的src
目录在你的类路径中。
It is not very strange. 这不是很奇怪。 You are trying to read the context from a file that does not exist.
您正尝试从不存在的文件中读取上下文。
ClassPathXmlApplicationContext
, true to its name, does not use the path as an absolute one but it seeks in the classpath. ClassPathXmlApplicationContext
,其名称为true,不使用路径作为绝对路径,但它在类路径中寻找。 You should use 你应该用
ApplicationContext context = new ClassPathXmlApplicationContext("/spring-cfg.xml");
NOTE: this will read the file not from src
but from the compiled classes (where it should have been copied to while compiling). 注意:这将不是从
src
读取文件,而是从编译的类中读取文件(在编译时它应该被复制到的位置)。
The message from the exception is correct, /home/user/IdeaProjects/Refactor/src/spring-cfg.xml
is not a classpath resource (looks like a regular path from your machine). 来自异常的消息是正确的,/
/home/user/IdeaProjects/Refactor/src/spring-cfg.xml
不是类路径资源(看起来像是来自机器的常规路径)。
I would advise using: ClassPathXmlApplicationContext("classpath:spring-cfg.xml")
as your config xml looks like being in your source folder. 我建议使用:
ClassPathXmlApplicationContext("classpath:spring-cfg.xml")
因为你的config xml看起来像是在你的源文件夹中。
I think this code will work 我认为这段代码会起作用
ApplicationContext context =
new FileSystemXmlApplicationContext("file:/home/user/IdeaProjects/Refactor/src/spring-cfg.xml");
You can find some helpful information here http://static.springsource.org/spring/docs/2.5.6/reference/resources.html 你可以在这里找到一些有用的信息http://static.springsource.org/spring/docs/2.5.6/reference/resources.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.