[英]Cannot load dispatcher-servlet.xml from src/main/resources
My spring mvc webapp had this structure (Maven project):我的 spring mvc webapp 有这个结构(Maven 项目):
src
- main
-- java
-- webapp
--- WEB-INF
--- web.xml
--- mvc-dispatcher-servlet.xml
Than I decided to write unit tests(junit).比我决定编写单元测试(junit)。 In an article I read that I should move the mvc-dispatcher-servlet.xml to src/main/resources so that I can access it here in my test in the ContextConfiguration annotation:
在我读到的一篇文章中,我应该将 mvc-dispatcher-servlet.xml 移动到 src/main/resources 以便我可以在我的测试中的 ContextConfiguration 注释中访问它:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/mvc-dispatcher-servlet.xml")
public class UserDaoTest {
@Autowired
private UserDao userDao;
@Test
public void testGetUserByUsername() throws Exception {
User admin = userDao.getUserByUsername("admin");
Assert.assertNotNull(admin);
}
}
That worked fine and my unit test was running successfull.效果很好,我的单元测试运行成功。 Than I started my application server to implement further features but than I got exceptions.
比我启动我的应用程序服务器来实现更多功能,但我遇到了异常。 I figured out, that after moving the mvc-dispatcher-servlet.xml from WEB-INF to src/main/resources I forgot to make the following change in my web.xml:
我发现,在将 mvc-dispatcher-servlet.xml 从 WEB-INF 移动到 src/main/resources 后,我忘记在 web.xml 中进行以下更改:
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- changed from /WEB-INF/mvc-dispatcher-servlet.xml -->
<param-value>classpath:mvc-dispatcher-servlet.xml</param-value>
</context-param>
But I still get the following exception:但我仍然收到以下异常:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
What did I do wrong?我做错了什么?
Please, consider this possible solution to your problem.请考虑这个可能的解决方案来解决您的问题。 Unfortunately, I can't conclude what was wrong with your way, but that solution should work.
不幸的是,我无法断定您的方式有什么问题,但该解决方案应该有效。
Move the description of contextConfigLocation from context-param tag to init-param tag inside servlet tag as follows:将 contextConfigLocation 的描述从 context-param 标签移动到 servlet 标签内的 init-param 标签,如下所示:
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- changed from /WEB-INF/mvc-dispatcher-servlet.xml -->
<param-value>classpath:mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.