Mah*_*leh 2 junit spring spring-mvc
我的applicationContext.xml,webmvc-config.xml位于WEB-INF/spring/applicationContext.xml中
当我尝试以下,它不加载,我得到 java.io.FileNotFoundException
@ContextConfiguration(locations = { "classpath:WEB-INF/spring/applicationContext.xml" })
Run Code Online (Sandbox Code Playgroud)
我正在使用spring 3,junit 4.7.
它通过复制resources文件夹中的applicationContext.xml来处理脏的变通方法,因此它是重复的.
我把装载改为:
@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
Run Code Online (Sandbox Code Playgroud)
我的web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- start up and shut down Spring's root WebApplicationContext (Interface to provide configuration for a web application) -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Central dispatcher for HTTP request handlers/controllers: take an incoming URI and find the right combination of handlers (generally methods on Controller classes)
and views (generally JSPs) that combine to form the page or resource that's supposed to be found at that location. -->
<servlet>
<servlet-name>p</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/webmvc-config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>p</servlet-name>
<url-pattern>/p/*</url-pattern>
</servlet-mapping>
<!-- allows one to specify a character encoding for requests.
This is useful because current browsers typically do not set a character encoding even if specified in the HTML page or form -->
<filter>
<filter-name>encoding-filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/applicationContext.xml
</param-value>
</context-param>
<!--
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
<!-- Based on the popular and very useful mod_rewrite for apache, UrlRewriteFilter is a Java Web Filter for any J2EE
compliant web application server (such as Resin or Tomcat), which allows you to rewrite URLs before they get to your
code. It is a very powerful tool just like Apache's mod_rewrite. -->
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
请告知更好的解决方案.
Web应用程序的类路径由
因此,如果要从类路径加载上下文文件,则需要在其中一个位置加载它.把它放入WEB-INF/classes/spring并加载它classpath:spring/applicationContext.xml,例如.
编辑:我刚刚意识到从JUnit测试加载上下文文件时遇到问题.答案是类似的.包含的目录WEB-INF肯定不在您的单元测试运行器的类路径中.单元测试运行器应该使用与应用程序服务器大致相同的类路径,因此该文件应该位于一个位置,使其在构建后转到JAR文件或测试的类路径中的目录.如果使用Maven,src/main/resources目录通常就是其中之一:该目录中的所有内容都转到target/classes目录,该目录位于单元测试运行器的类路径中.