5

出于某种原因(shiro 过滤器),我将应用程序上下文文件保存在 WEB-INF 文件夹中。当我运行 tomcat 时一切正常,但是当我尝试使用以下命令从控制器获取应用程序上下文时:

context = new ClassPathXmlApplicationContext(fileContext);

我总是收到这个例外:

IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist

似乎在 ecplise 下我无法将 WEB-INF 包含在类路径下。我在stackoverflow中查看了很多问题,但我还没有找到解决方案。

如果我将 applicationContext.xml 文件移动到 src/main/java 文件夹下,我可以获取上下文,但是定义到 web.xml 文件中的 shiro 文件无法看到在 applicationContext 文件下定义的 shiro bean(我加倍检查并且 bean 工作正常)。如何告诉 web.xml 从 src/main/java 获取内容?或者,我怎样才能到达 applicationContext.xml

4

4 回答 4

2

WEB-INF 不在您的 CLASSPATH 中。WEB-INF/classes 是。那么为什么不将它放在源文件夹中并打包应用程序呢?

于 2013-04-10T21:13:44.380 回答
1

不要ApplicationContext在控制器中创建实例。春天DispatcherServlet已经为你创造了一个。您需要做的就是使用@Autowired.

于 2013-04-11T03:00:53.480 回答
1

采用

context = new FileSystemXmlApplicationContext(fileContext);

代替

context = new ClassPathXmlApplicationContext(fileContext);
于 2013-04-11T06:19:05.150 回答
0

问题已解决,移动 WEB-INF/classes 下的所有配置文件并添加前缀类路径:

<import resource="classpath:spring-data.xml"/> 

感谢大家的帮助!我非常感谢!

干杯,安德里亚

于 2013-04-11T09:05:52.040 回答