0

我有以下 webApp 文件夹结构:

在此处输入图像描述

我想手动加载弹簧上下文。

我写了以下代码:

ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");

当上面的代码调用时,我看到以下异常消息:

java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist

如何重写我的代码以避免此异常?

PS 我不想移动我的xml文件。

聚苯乙烯

new ClassPathXmlApplicationContext("WEB-INF/applicationContext.xml")  

也不起作用,尽管在 web.xml 中编写了

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>

它有效

4

1 回答 1

0

当您提供此位置时

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>

资源是相对于 Servlet 上下文路径解析的。

当您提供

new ClassPathXmlApplicationContext("WEB-INF/applicationContext.xml")  

你告诉 Spring 在类路径中找到给定的资源。在您的情况下,它可能不存在(WEB-INF 通常不添加到类路径 afaik)。

将它添加到类路径或将applicationContext.xml文件移动到类路径上的不同位置并在构造函数中使用该路径。

于 2015-06-05T22:13:09.087 回答