gpt4 book ai didi

spring - 无法在测试类中导入 applicationContext

转载 作者:行者123 更新时间:2023-12-04 19:50:27 26 4
gpt4 key购买 nike

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

它通过复制 resources 文件夹中的 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>

请提出更好的解决方案。

最佳答案

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 目录,该目录位于单元测试运行器。

关于spring - 无法在测试类中导入 applicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7418106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com