无法在测试类中导入 applicationContext

发布于 2024-12-04 16:41:22 字数 4344 浏览 2 评论 0原文

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

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

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

我是使用 spring 3,junit 4.7。

它通过复制资源文件夹中的 applicationContext.xml 来解决脏问题,因此它是重复的。

我将加载更改为:

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

我的 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>

请建议更好的解决方案。

my applicationContext.xml,webmvc-config.xml are in WEB-INF/spring/applicationContext.xml

when i try the following, it doesn't load, and i get java.io.FileNotFoundException

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

i am using spring 3, junit 4.7.

it works with the dirty workaround by copying applicationContext.xml in resources folder, so it's duplicated.

and i changed the loading to:

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

please advise a better solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赤濁 2024-12-11 16:41:22

Web 应用程序的类路径由

  • WEB-INF/classes 和
  • WEB-INF/lib 下的所有 jar

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

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

The classpath of a web application is composed of

  • WEB-INF/classes
  • all the jars under WEB-INF/lib

So, if you want to load the context file from the classpath, you need it in one of those places. Put it in WEB-INF/classes/spring and load it using classpath:spring/applicationContext.xml for example.

EDIT: I just realized that you have a problem loading the context file from a JUnit test. The answer is similar, though. The directory containing WEB-INF certainly is not in the classpath of your unit test runner. The unit test runner should use more or less the same classpath as the application server, so the file should be in a location that makes it go to a JAR file or a directory that is in the classpath of the test after the build. If using Maven, the src/main/resources directory is normally one of them: everything in that directory goes to the target/classes directory, which is in the classpath of the unit test runner.

七分※倦醒 2024-12-11 16:41:22

好吧,最后我能够按如下方式执行此操作(基于 spring roo 生成的应用程序):

我将 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>

和单元测试中班级:

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

well, finally i was able to do it as follows (based on spring roo generated application):

i put my applicationContex.xml file inside the directory:

src/main/resources/META-INF/spring

and in the web.xml:

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

and in the unit test class:

@ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" })
究竟谁懂我的在乎 2024-12-11 16:41:22

我将应用程序上下文文件保存在 (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>

这就是我的测试配置的样子

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
@Transactional
public class MyTest 
{
}

I keep my application context files in (src/main/resources) and add the class path suffix to access them from test or web.xml

To access the context file that is located in src/main/resources from (web.xml), add this config to your 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>

This is how my test config looks like

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
@Transactional
public class MyTest 
{
}
故人爱我别走 2024-12-11 16:41:22

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

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

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

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

A different solution is to add the applicationContext to main/src/resources.

Also add (a second one) application context (for the servlet) to META-INF, with only one line that imports the first context from the classpath

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

Then you can import the first one into the test, and there are no duplicate definitions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文