Ne pouvez pas Importer des applicationContext dans la classe de test

mon applicationContext.xml,webmvc-config.xml sont dans WEB-INF/spring/applicationContext.xml

quand j'ai essayer le suivant, il ne charge pas, et je reçois java.io.FileNotFoundException

@ContextConfiguration(locations = { "classpath:WEB-INF/spring/applicationContext.xml" })

je suis en utilisant le printemps 3, junit 4.7.

il fonctionne avec le sale contournement par la copie applicationContext.xml dans le dossier de ressources, il est donc double.

et j'ai changé le chargement:

@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })

mon 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>

veuillez indiquer une meilleure solution.

Qu'est-ce que votre classpath de l'endroit? Si c'est une servlet, il est normalement WEB-INF/classes cela signifie que vous avez à garder votre applicationContext fichier dans WEB-INF/classes/spring/applicationContext.xml et l'utilisation de la configuration que @ContextConfiguration(locations = { "classpath:spring/applicationContext.xml" })

OriginalL'auteur Mahmoud Saleh | 2011-09-14