English 中文(简体)
Spring,正在使用新的ClassPathXmlApplicationContext,并在找不到applicationContext.xml等时出错?
原标题:Spring, using new ClassPathXmlApplicationContext and getting error being unable to find applicationContext.xml and others?

我正在努力学习本教程:http://www.vogella.de/articles/SpringDependencyInjection/article.html在我的应用程序中使用注释依赖项注入。我像在教程中一样设置了bean等,然后试图在我的MainController类(一个控制器类,用于为我的spring-web mvc应用程序生成特定页面)中获取bean的实例。。我一直在

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

我在我的主控制器中执行此操作:

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

    BeanFactory factory = context;
    BeanIRPlus beanirPlus = (BeanIRPlus) factory
            .getBean("BeanIRPlus");
    IRPlusInterface irPlus = beanirPlus.getIRPlus();

我对此搜索了又搜索,但还没有找到解决我问题的答案。webapp/WEB-INF/中的applicationContext和我的spring应用程序似乎在其他方面工作,因为在此之前它正在处理请求等。我尝试过将applicationContext.xml放入WEB-INF类中,但仍然一无所获。有没有什么变通方法可以让它不以这种方式搜索路径,因为我认为它是在进行相对路径搜索。谢谢你的建议

最佳回答

这不是一个直接的答案,但答案如下。

您提到的教程是针对独立应用程序而非web应用程序中的依赖项注入的。在web应用程序的情况下,spring会自动加载上下文文件并初始化bean。因此,您不需要MainController中指定的任何行。

相反,您可以这样做,在控制器中使用beanIRPLusbean。

@Autowired
private BeanIRPlus beanIRPlus;
问题回答

暂无回答




相关问题
array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签