java 未找到 applicationContext 类路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15936187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
applicationContext classpath not found
提问by Andrea Girardi
For some reason (a shiro filter) I saved my application context file in WEB-INF folder. Everything works when I run tomcat but, when I try to get an application context from a controller using :
由于某种原因(shiro 过滤器),我将应用程序上下文文件保存在 WEB-INF 文件夹中。当我运行 tomcat 时一切正常,但是,当我尝试使用以下命令从控制器获取应用程序上下文时:
context = new ClassPathXmlApplicationContext(fileContext);
I receive always this exception:
我总是收到这个例外:
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
It seems that under ecplise I'm not able to include WEB-INF under classpath. I took a look to a lot questions here in stackoverflow but I didn't find yet a solution.
似乎在 ecplise 下我无法在类路径下包含 WEB-INF。我在stackoverflow中查看了很多问题,但我还没有找到解决方案。
If I move the applicationContext.xml file under src/main/java folder, I'm able to get the context but, the shiro filder defined into web.xml file is not able to see the shiro bean defined under applicationContext file (I double checked and the bean is correctly worked). How can I tell to web.xml to get content from src/main/java? Or, how can I reach the applicationContext.xml
如果我将 applicationContext.xml 文件移动到 src/main/java 文件夹下,我可以获得上下文,但是,定义到 web.xml 文件中的 shiro filder 无法看到 applicationContext 文件下定义的 shiro bean(我加倍检查并且 bean 正常工作)。如何告诉 web.xml 从 src/main/java 获取内容?或者,我怎样才能到达 applicationContext.xml
采纳答案by Andrea Girardi
Problem has been solved moving all configuration file under WEB-INF/classes and adding the prefix classpath:
问题已解决,移动 WEB-INF/classes 下的所有配置文件并添加前缀类路径:
<import resource="classpath:spring-data.xml"/>
thanks to all for the help! I really appreciate that!
感谢大家的帮助!我真的很感激!
cheers, Andrea
干杯,安德里亚
回答by maggu
WEB-INF is not in your CLASSPATH. WEB-INF/classes is. So why dont you put it in a source folder and package the application?
WEB-INF 不在您的 CLASSPATH 中。WEB-INF/classes 是。那么为什么不把它放在一个源文件夹中并打包应用程序呢?
回答by subodh
use
利用
context = new FileSystemXmlApplicationContext(fileContext);
instead of
代替
context = new ClassPathXmlApplicationContext(fileContext);
回答by cRx
Do not create an instance of ApplicationContext
in your controller. The spring DispatcherServlet
already creates one for you. All you need to do is access all bean declarations in you application context file using @Autowired
.
不要ApplicationContext
在你的控制器中创建一个实例。春天DispatcherServlet
已经为你创造了一个。您需要做的就是使用@Autowired
.