通过Manifest文件将Spring配置文件添加到类路径

发布于 2024-09-25 18:48:15 字数 514 浏览 1 评论 0原文

我有一个 WAR 项目,我试图将 spring 配置文件存储在 /WebContent/WEB-INF/Spring/ 文件夹中(按照我的雇主的要求),但是当我这样做时,我还没有找到能够引用的方法我的 junit 测试中的这些文件。今天有人告诉我,我可以修改 MANIFEST.MF 文件,以便包含 spring 配置文件,但我无法成功引用清单文件中的文件。

我的第一个问题是是否可以从我的 junits 引用 /WebContent/WEB-INF/Spring/ 文件夹中的文件?如果可能的话,清单文件如何知道在哪里寻找文件?链接是相对的吗?

我的 WAR 的文件夹结构如下(结构由我工作的公司定义):

WARProject
 - src\
 - WebContent\
 - - WEB-INF\
 - - - Spring\
 - - - - App-config.xml
 - - - - Security.xml
 - - META-INF\
 - - - MANIFEST.MF

感谢您提供的任何指导。

杰里米

I have a WAR project and I am trying to store spring configuration files within the /WebContent/WEB-INF/Spring/ folder (as required by my employer), but when I do that I have not found a way to be able to reference those files within my junit tests. I was told today that I can modify my MANIFEST.MF file so that the spring configuration files are included, but I have not been able to successfully reference the files from the manifest file.

My first question is whether or not it is possible to reference files in the /WebContent/WEB-INF/Spring/ folder from my junits? If it is possible, how to the manifest file know where to look for the files? Are the links relative?

The folder structure for my WAR is as follows (structure is defined by the company I work for):

WARProject
 - src\
 - WebContent\
 - - WEB-INF\
 - - - Spring\
 - - - - App-config.xml
 - - - - Security.xml
 - - META-INF\
 - - - MANIFEST.MF

I appreciate any guidance you can provide.

Jeremy

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

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

发布评论

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

评论(2

日裸衫吸 2024-10-02 18:48:15

如果您使用 Spring 2.5 或更高版本并且 Spring的TestContext框架,那么你可以简单地用@ContextConfiguration(locations = {path/to/file})注释测试类

如果你自己加载一个ApplicationContext,那么只需确保 /WebContent/WEB-INF/Spring/ 位于类路径上,并使用 classpath:contextname.xml 加载该文件。

If you are using Spring 2.5 or later and Spring's TestContext framework, then you can simply annotate the test class with @ContextConfiguration(locations = {path/to/file})

If you are loading an ApplicationContext yourself, then just make sure that /WebContent/WEB-INF/Spring/ is on the classpath and load the file with classpath:contextname.xml.

﹎☆浅夏丿初晴 2024-10-02 18:48:15

无论如何,无论 Spring 版本如何,您都可以使用 FileSystemXmlApplicationContext 类加载应用程序上下文进行测试,并传递字符串 "file:WebContent/WEB-INF/Spring/App -config.xml" 作为 'configLocation' 参数,这将从 XML 配置中加载您的应用程序上下文。

考虑到这一点,Matt 确实有一个好主意,如果您能够使用基于注释的测试,则应该将 @ContextConfiguration 注释与 @RunWith(SpringJUnit4TestRunner.class ) 注解,让​​ Spring 的测试框架自动为您加载上下文,在这种情况下,您还可以在 @ContextConfiguration 注解中使用上面的字符串。您不需要在类路径中包含应用程序上下文,也不需要仅为了测试而将 /WebContent/WEB-INF/Spring/ 添加到类路径中。

In any case, regardless of version of Spring, you can use the FileSystemXmlApplicationContext class to load your application context for testing, and pass the string "file:WebContent/WEB-INF/Spring/App-config.xml" in as the 'configLocation' argument, and this will load your application context for you from the XML configuration.

With that in mind, Matt does have a good idea in that if you are capable of using annotation based testing, you should use the @ContextConfiguration annotation in conjunction with the @RunWith(SpringJUnit4TestRunner.class) annotation to have Spring's testing framework load the context for you automatically, in which case you could also use the string above in the @ContextConfiguration annotation. You don't need to have the application context in your classpath, nor do you need to add /WebContent/WEB-INF/Spring/ to your classpath just for testing.

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