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