在Spring中获取FileNotFoundException [英] Getting FileNotFoundException in Spring

查看:137
本文介绍了在Spring中获取FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用BeanFactory创建bean,但我得到一个例外: java.io.FileNotFoundException:\\WEB-INF \ businesscaliber-servlet.xml

I want to create bean using BeanFactory, but I am getting an exeception: java.io.FileNotFoundException: \\WEB-INF\businesscaliber-servlet.xml.

Resource res = new FileSystemResource("//WEB-INF//businesscaliber-servlet.xml");
BeanFactory factory = new XmlBeanFactory(res);
if (factory != null && beanId != null) {
    obj = factory.getBean(beanId);
}

他使用此工作

ApplicationContext ctx = new FileSystemXmlApplicationContext(classpath *:/ WEB-INF / businesscaliber-servlet.xml);

ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath*:/WEB-INF/businesscaliber-servlet.xml");

推荐答案

我认为你需要指定一个绝对路径,而不是指向 FileSystemResource

I believe you need to specify an absolute path and not a Web application relative path to FileSystemResource.

尝试使用 ServletContextResource


资源 实施
ServletContext 资源,
解释
Web应用程序根目录中的相对路径。

Resource implementation for ServletContext resources, interpreting relative paths within the web application root directory.

唯一的问题是你需要 ServletContext 所以:

The only issue is you need the ServletContext so:

ServletContext servletContext = ...
Resource res = new ServletContextResource(servletContext,
  "/WEB-INF/businesscaliber-servlet.xml");
BeanFactory factory = new XmlBeanFactory(res);
if (factory != null && beanId != null) {
    obj = factory.getBean(beanId);
}

值得注意的是,理想情况下你会从 ApplicationContext 。来自 4.4资源加载器 Spring Reference

It's worth noting that ideally you would retrieve this from an ApplicationContext. From 4.4 Resource Loader of the Spring Reference:


Resource template = ctx.getResource("some/resource/path/myTemplate.txt);

将返回的是
ClassPathResource ;如果针对
FileSystemXmlApplicationContext
实例执行相同的
方法,您将获得$
FileSystemResource 。对于
WebApplicationContext ,你得到
返回 ServletContextResource ,以及
等等。

What would be returned would be a ClassPathResource; if the same method was executed against a FileSystemXmlApplicationContext instance, you'd get back a FileSystemResource. For a WebApplicationContext, you'd get back a ServletContextResource, and so on.

因此,您可以在
中加载资源适合特定
应用程序上下文的时尚。

As such, you can load resources in a fashion appropriate to the particular application context.

所以这是检索资源的首选方法。

So this is the preferred method of retrieving resources.

或者从 / WEB-INF / 技术上在类路径中,您可以使用 classpath:前缀(根据您的评论)或使用 ClassPathXmlApplicationContext (它将自动返回类路径资源)。

Alternatively since /WEB-INF/ is technically in the classpath you can use the classpath: prefix (as per your comment) or use ClassPathXmlApplicationContext (which will automatically return classpath resources).

此外,无需添加双正斜杠。不知道你为什么要这样做。也许是双反斜杠的延续,这是必要的吗?

Also theres no need to put double forward slashes in. Not sure why you're doing this. Perhaps a holdover from double backslashes, which are necessary?

这篇关于在Spring中获取FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆