bla*_*ang 2 java spring classpath classloader applicationcontext
我需要使用当前Maven构建中的"main"applicationContext -a.xml创建一个ApplicationContext.另一个来自另一个maven构建的连接类,并在Maven依赖项包含的jar中预设.
这个想法:
ApplicationContext context = new ClassPathXmlApplicationContext( new String[] {
"classpath*:applicationContext-*.xml"});
Run Code Online (Sandbox Code Playgroud)
这应该从Classpath加载applicationContext-a.xml,因为它在同一个Project中.这有效.
然后应该从dependency-jar加载applicationContext-b.xml. 这不起作用.
注意
"classpath*:applicationContext-*.xml"
Run Code Online (Sandbox Code Playgroud)
只匹配直接类路径中的XML,jar中没有任何内容.
我发现了什么:
ApplicationContext context = new ClassPathXmlApplicationContext( new String[] {
"classpath*:applicationContext-*.xml", "classpath*:applicationContext-b.xml"});
Run Code Online (Sandbox Code Playgroud)
这是有效的,但前提是我可以在jar:applicationContext-b.xml中明确告诉xml的文件名
我还需要它来进行集成测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"**/applicationContext*.xml"})
public class TestClass { ... }
Run Code Online (Sandbox Code Playgroud)
最好的想法可能是自定义装载机?必须有一种方法来使这个模式工作......
前段时间有一个解决方案,反之亦然:它只从jar获取applicationContext.xml.如果类路径中有另一个,它只匹配此文件.
在classpath*:我认为是有限制的根文件夹下的文件不起作用.尝试将文件移动到文件夹下 - 类似于spring/application-a.xml和spring/application-b.xml.然后你可以有一条路classpath*:/spring/application-*.xml.