Spring Can';t在测试类中导入applicationContext

Spring Can';t在测试类中导入applicationContext,spring,spring-mvc,junit,Spring,Spring Mvc,Junit,我的applicationContext.xml、webmvc-config.xml位于WEB-INF/spring/applicationContext.xml中 当我尝试以下操作时,它不会加载,我得到java.io.FileNotFoundException @ContextConfiguration(locations = { "classpath:WEB-INF/spring/applicationContext.xml" }) 我使用的是Spring3,JUnit4.7 它通过复制r

我的applicationContext.xml、webmvc-config.xml位于WEB-INF/spring/applicationContext.xml中

当我尝试以下操作时,它不会加载,我得到
java.io.FileNotFoundException

@ContextConfiguration(locations = { "classpath:WEB-INF/spring/applicationContext.xml" })
我使用的是Spring3,JUnit4.7

它通过复制resources文件夹中的applicationContext.xml来解决脏的解决方法,因此它是重复的

我将加载更改为:

@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
my 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>

org.springframework.web.context.ContextLoaderListener
P
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/webmvc-config.xml
1.
P
/p/*
编码过滤器
org.springframework.web.filter.CharacterEncodingFilter
编码
utf-8
强制编码
真的
编码过滤器
/*
要求
向前地
上下文配置位置
/WEB-INF/spring/applicationContext.xml
URL重写过滤器
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
URL重写过滤器
/* 

请提供更好的解决方案。

web应用程序的类路径由

  • WEB-INF/课程
  • WEB-INF/lib下的所有JAR
因此,如果要从类路径加载上下文文件,需要将其放在这些位置之一。将它放入
WEB-INF/classes/spring
中,并使用
classpath:spring/applicationContext.xml
加载它


编辑:我刚刚意识到从JUnit测试加载上下文文件时遇到问题。不过,答案是相似的。包含
WEB-INF
的目录肯定不在单元测试运行程序的类路径中。单元测试运行程序应该使用与应用程序服务器大致相同的类路径,因此该文件应该位于一个位置,使其在构建后可以转到JAR文件或位于测试类路径中的目录。如果使用Maven,则
src/main/resources
目录通常是其中之一:该目录中的所有内容都会转到
target/classes
目录,该目录位于单元测试运行程序的类路径中。

我将应用程序上下文文件保存在(src/main/resources)中并添加类路径后缀以从test或web.xml访问它们

要从(web.xml)访问位于src/main/resources中的上下文文件,请将此配置添加到web.xml

 <context-param>
  <description>
           Context parameters for Spring ContextLoaderListener     
      </description>
   <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath*:applicationContext.xml
    </param-value>
  </context-param>

  <listener>
     <listener-class>
           org.springframework.web.context.ContextLoaderListener
        </listener-class>
  </listener>

最后,我可以按如下方式完成(基于SpringRoo生成的应用程序):

我将applicationContex.xml文件放在目录中:

src/main/resources/META-INF/spring
在web.xml中:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

另一种解决方案是将applicationContext添加到main/src/resources

还要将(第二个)应用程序上下文(用于servlet)添加到META-INF,其中只有一行从类路径导入第一个上下文

  <import resource="classpath:/mypackage/my-servlet.xml"/>


然后您可以将第一个导入测试,并且没有重复的定义。

您的类路径位置是什么?如果它是一个servlet,则通常是
WEB-INF/classes
,这意味着您必须将应用程序上下文文件保存在
WEB-INF/classes/spring/applicationContext.xml
中,并将配置用作
@ContextConfiguration(位置={“classpath:spring/applicationContext.xml”)
抱歉,我不明白您将我的上下文放在以下位置是什么意思:WEB-INF/classes/spring,您的意思是在WEB-INF中创建一个新文件夹,还是在target/classes中创建一个名为spring的新文件夹,还是什么,我无法获取。target/classes是Maven编译类并复制资源的目录。编译之后,您的构建可能会生成一个war文件,或者一个分解的war文件。此war文件或分解的war文件是应用程序服务器用于运行web应用程序的文件。应用服务器并不关心您的源代码是如何组织的,也不关心您使用哪个构建工具来构建war。重要的是war文件的结构,它必须遵守JEE规范。因此,将文件放在需要的位置,以便在构建之后,它位于war中的WEB-INF/classes/spring中。如果使用Maven,将其放在src/resources/spring目录中,应该使其在构建之后转到WEB-INF/classes/spring。但这可能取决于您的pom.xml。当我这样做时,我能够成功地运行测试,但无法运行应用程序,我遇到以下异常:java.io.FileNotFoundException:无法打开ServletContext资源[/resources/spring/applicationContext.xml]我认为@JB是对的,您只需要添加(classpath*:context.xml)在您的web.xml中,这应该可以解决问题,我将我的上下文文件保存在resources目录中,以便从我的测试中访问它们。只对applicationContext或所有spring配置文件(如mvc配置、安全性、servlet)执行OVE。我的意思是,我是否应该对mvc配置执行相同的操作:p org.springframework.web.servlet.DispatcherServletcontextConfigLocation classpath*:webmvc config.xml 1我对所有配置文件都这样做,但我认为这与我个人的偏好有关,即将所有配置文件保存在src/main/resources目录中。当我制作类似于webmvc的配置文件时,我将把它留给您,让您选择mvc/security/servlet配置文件的位置-config.xml如上所述,应用程序没有运行,我找不到任何异常,它构建和启动时没有错误,但没有显示任何内容。我有一个应用程序可以使用此配置,只是为了测试它是否实际查找文件,而不是webmvc-config.xml。如果输入错误的名称,您应该会得到一个未找到的文件异常。如果至少它证明了配置不是问题所在。您还可以在log4j中设置从spring到info的日志记录,以便在容器启动时获得尽可能多的信息。
@ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" })
  <import resource="classpath:/mypackage/my-servlet.xml"/>