简体   繁体   English

如何从jar文件加载Spring context xml

[英]How to load Spring context xml from a jar file

I am running a Junit (SpringJUnit4ClassRunner) to access some spring beans in my integration test. 我正在运行Junit(SpringJUnit4ClassRunner)以在集成测试中访问一些Spring bean。

I need to load a few xml files via @ContextConfiguration, those files are deployed by a external approach to my Tomcat directly via a jar file. 我需要通过@ContextConfiguration加载一些xml文件,这些文件是通过外部方法直接通过jar文件部署到Tomcat的。 -- In other words, these files are in: -换句话说,这些文件位于:

my_tomcat_home_path/webapps//WEB-INF/lib/external.jar my_tomcat_home_path / web应用// WEB-INF / LIB / external.jar

such as

my_tomcat_home_path/webapps//WEB-INF/lib/external.jar/a.suffix.xml 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 my_tomcat_home_path / web应用// WEB-INF / LIB / external.jar / b.suffix.xml

... ...

I put Tomcat path into my eclipse classpath, then if I also include the that into my eclipse classpath, then the following code in my JUnit works 我将Tomcat路径放入了eclipse类路径中,然后,如果我还将Tomcat路径也包括进了eclipse类路径中,那么我的JUnit中的以下代码将起作用

@ContextConfiguration(locations = {"classpath:*suffix.xml"})

However, if I don't put that into my eclipse classpath (because the jar file name may change from time to time), then my following code does NOT work (but Tomcat home "" is still in eclipse classpath): 但是,如果我不将其放入eclipse类路径中(因为jar文件名可能会不时更改),那么我的以下代码将无法正常工作(但是Tomcat主页“”仍在eclipse类路径中):

@ContextConfiguration(locations = {"classpath:/webapps/<my_app>/WEB-INF/lib/external.jar/*suffix.xml"})

Could somebody please help me out? 有人可以帮我吗?

Thanks a lot!! 非常感谢!!

Additional try ------------------------------- 额外尝试-------------------------------

I tried to use the package path inside the jar, instead of using the external.jar itself. 我尝试使用jar中的包路径,而不是使用external.jar本身。 The the code looks like the following: 该代码如下所示:

@ContextConfiguration(locations = {"classpath:/webapps/<my_app>/WEB-INF/lib/path_inside_the_external_jar/*suffix.xml"})

In this case, I no longer get "XML not found because file does not exist" problem. 在这种情况下,我不再遇到“找不到XML,因为文件不存在”的问题。 Instead, I get the following errors: 相反,我收到以下错误:

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

Thanks a lot to Didxga's help!! 非常感谢Didxga的帮助!

I tried to remove the "/webapps//WEB-INF/lib", and only leave the package path to the *suffix.xml (which are actually *.hbm.xml), then the @ContextConfiguration(locations looks like the following: 我试图删除“ / webapps // WEB-INF / lib”,仅将包路径保留为* suffix.xml(实际上是* .hbm.xml),然后@ContextConfiguration(位置如下所示) :

"classpath:/internal_path_within_jar/*hbm.xml"

at first, which is still not working. 起初,它仍然无法正常工作。

Afterward, I tried the modify the classpath pattern and the following code ALMOST works 之后,我尝试修改类路径模式,以下代码ALMOST起作用

"classpath*:/internal_path_within_jar/*hbm.xml"

This time, since hbm.xml files are loaded, and I get the following error: 这次,由于已加载hbm.xml文件,并且出现以下错误:

Caused by: java.net.UnknownHostException: hibernate.sourceforge.net
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:411)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:525)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:208)
at sun.net.www.http.HttpClient.New(HttpClient.java:291)
at sun.net.www.http.HttpClient.New(HttpClient.java:310)

However, I think now it is a different problem because it is hibernate DTD load problem... Maybe I should NOT load hbm.xml in this approach... I was forced to do so, because those hbm.xml files are generated during the deployment progress of my application and will only appear in the Tomcat's websapp/my_app/external.jar and NOwhere else... 但是,我认为这是一个不同的问题,因为它是休眠DTD加载问题...也许我不应该以这种方式加载hbm.xml ...我被迫这样做,因为这些hbm.xml文件是在我的应用程序的部署进度,并且只会显示在Tomcat的websapp / my_app / external.jar中,而在其他任何地方都不会出现...

Hmm, so I need work on that part... but how to include a jar into my eclipse path with changing name but a certain pattern? 嗯,所以我需要在那部分上工作...但是如何在罐子的日食路径中加入一个罐子,而罐子的名字却要改变但要有一定的样式? Such as *-dao.jar. 如* -dao.jar。 OMG... 我的天啊...

Anyway, I suppose the problem reported in this post should be resolved. 无论如何,我想这篇文章中报告的问题应该得到解决。

Thanks a lot, didxga! 非常感谢,didxga!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM