我正在运行Junit(SpringJUnit4ClassRunner)来访问我的集成测试中的一些spring bean。如何从jar文件加载Spring上下文xml
我需要通过@ContextConfiguration加载几个xml文件,这些文件通过外部方法直接通过jar文件部署到我的Tomcat。 - 换句话说,这些文件是:
my_tomcat_home_path/web应用// WEB-INF/LIB/external.jar
如
my_tomcat_home_path/web应用// WEB-INF/LIB/external.jar/a.suffix.xml
my_tomcat_home_path/webapps中// WEB-INF/lib目录/ external.jar/b.suffix.xml
...
我把Tomcat的路径为我的ECL IPSE类路径,那么,如果我还包括该到我的Eclipse类路径,然后在我的JUnit下面的代码工作
@ContextConfiguration(locations = {"classpath:*suffix.xml"})
但是,如果我不将它放入我的日食类路径(因为jar文件名可能改变不时),那么我下面的代码不能正常工作(但Tomcat的家“”仍然在eclipse classpath中):
@ContextConfiguration(locations = {"classpath:/webapps/<my_app>/WEB-INF/lib/external.jar/*suffix.xml"})
可能有人请帮助我吗?
非常感谢!
额外的尝试-------------------------------
我试图使用内部的包路径jar,而不是使用external.jar本身。该代码如下所示:
@ContextConfiguration(locations = {"classpath:/webapps/<my_app>/WEB-INF/lib/path_inside_the_external_jar/*suffix.xml"})
在这种情况下,我不再收到“XML未找到,因为文件不存在”的问题。相反,我得到以下错误:
Caused by: java.io.FileNotFoundException: class path resource [webapps/my_app/WEB-INF/lib/path_inside_the_external_jar/] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:163)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.isJarResource(PathMatchingResourcePatternResolver.java:406)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:338)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:276)
at org.springframework.context.support.AbstractApplicationContext.getResources(AbstractApplicationContext.java:1018)
at org.springframework.context.support.GenericApplicationContext.getResources(GenericApplicationContext.java:192)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:177)
... 23 more
在您的位置配置,请不要使用external.jar而是使用* suffix.xml的包路径 – didxga
嗨didxga,谢谢你的提示!我试了一下,得到的URL不存在的错误,我已经更新了帖子来显示这个尝试。 –
尝试从您的位置配置中移除“/ webapps//WEB-INF/lib”。 –
didxga