春天,使用新的ClassPathXmlApplicationContext和得到错误是无法找到的applicationContext.xml和其他人? [英] Spring, using new ClassPathXmlApplicationContext and getting error being unable to find applicationContext.xml and others?
问题描述
我想按照这个教程: http://www.vogella.de/文章/ SpringDependencyInjection / article.html 使用注释依赖注入我的应用程序。我成立了绿豆等像教程,然后我试图我MainController类(处理产生我和Spring Web MVC应用程序特定的页面控制器类)中得到bean的实例。我不断收到
I am trying to follow this tutorial: http://www.vogella.de/articles/SpringDependencyInjection/article.html to use annotation dependency injection in my application. I set up the bean, etc like in the tutorial and then am trying to get an instance of the bean within my MainController class (a controller class that handles generating a specific page for my spring web mvc app).. I keep getting
SEVERE: Servlet.service() for servlet spring threw exception
java.io.FileNotFoundException: class path resource [WEB-INF/applicationContext.xml] cannot be opened because it does not exist
我在我的MainController这样做的:
I am doing this in my MainController:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanFactory factory = context;
BeanIRPlus beanirPlus = (BeanIRPlus) factory
.getBean("BeanIRPlus");
IRPlusInterface irPlus = beanirPlus.getIRPlus();
我已经搜查,搜查就这个问题和尚未找到能解决我的问题的答案。我在的webapp / WEB-INF的applicationContext /和我的春天应用程序似乎是另有工作,因为它是在此之前处理请求,等等。我试图把applicationContext.xml中的WEB-INF类,但仍然一无所获。有任何解决方法,使这个,因为我觉得它做的相对路径搜索无法搜索的路径这种方式。感谢您的任何建议。
I have searched and searched on this and yet to find an answer that fixes my problem. My applicationContext in in webapp/WEB-INF/ and my spring app seems to be working otherwise as it was handling requests, etc before this. I have tried putting the applicationContext.xml in WEB-INF classes but still nothing. Is there any workaround to make this not search the path this way as I think its doing a relative path search. Thanks for any advice
推荐答案
不是一个直接的答案,但在这里不用。
Not a direct answer, but here goes.
您提到的教程是一个独立的应用程序依赖注入,而不是一个Web应用程序。在Web应用程序的情况下,弹簧会自动加载上下文文件并初始化豆。所以,你不需要任何指定的行 MainController
。
The tutorial you have referred is for dependency injection in a standalone application and not a web application. In case of web application, spring automatically loads the context files and initializes the beans. So you would not need any of the lines specified in the MainController
.
相反,你可以做这样的事情在你的控制器使用beanIRPlus豆。
Instead, you could do something like this to use beanIRPlus bean in your controller.
@Autowired
private BeanIRPlus beanIRPlus;
这篇关于春天,使用新的ClassPathXmlApplicationContext和得到错误是无法找到的applicationContext.xml和其他人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!