无法在测试类中导入 applicationContext
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Web 应用程序的类路径由
组成,因此,如果要从类路径加载上下文文件,则需要将其放在这些位置之一。将其放入
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
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 usingclasspath: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, thesrc/main/resources
directory is normally one of them: everything in that directory goes to thetarget/classes
directory, which is in the classpath of the unit test runner.好吧,最后我能够按如下方式执行此操作(基于 spring roo 生成的应用程序):
我将 applicationContex.xml 文件放在目录中:
和 web.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:
and in the web.xml:
and in the unit test class:
我将应用程序上下文文件保存在 (src/main/resources) 中,并添加类路径后缀以从 test 或 web.xml 访问它们
。要从 (web.xml) 访问位于 src/main/resources 中的上下文文件,将此配置添加到您的 web.xml
这就是我的测试配置的样子
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
This is how my test config looks like
另一种解决方案是将 applicationContext 添加到 main/src/resources 中。
还将(第二个)应用程序上下文(用于 servlet)添加到 META-INF,仅用一行从类路径导入第一个上下文
然后您可以将第一个上下文导入到测试中,并且没有重复的定义。
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
Then you can import the first one into the test, and there are no duplicate definitions.