【问题标题】:Spring ApplicationContext with multiple XML Files from Jar带有来自 Jar 的多个 XML 文件的 Spring ApplicationContext
【发布时间】:2012-05-18 10:22:07
【问题描述】:

我需要使用当前 Maven 构建中的“主要”applicationContext-a.xml 创建一个 ApplicationContext。另一个连接来自另一个 maven 构建的类,并预设在 Maven 依赖项包含的 jar 中。

这里的想法:

ApplicationContext context = new ClassPathXmlApplicationContext( new String[] {
                "classpath*:applicationContext-*.xml"});

这应该从 Classpath 加载 applicationContext-a.xml,因为它在同一个项目中。 这行得通。

那么 applicationContext-b.xml 应该从dependency-jar 中加载。 这不起作用。

注意

"classpath*:applicationContext-*.xml"

仅匹配直接类路径中的 XML,不匹配 jar 中的任何内容。

我发现了什么:

ApplicationContext context = new ClassPathXmlApplicationContext( new String[] {
                "classpath*:applicationContext-*.xml", "classpath*:applicationContext-b.xml"});

这可行,但前提是我可以明确告诉 jar 中 xml 的文件名:applicationContext-b.xml

我还需要它来进行集成测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"**/applicationContext*.xml"})
public class TestClass { ... }

最好的主意可能是自定义加载器? 必须有办法让这个 Pattern 工作......

前段时间有一个solution,反之亦然:它只从jar 中获取applicationContext.xml。如果类路径中有其他人,则仅在此文件上匹配。

【问题讨论】:

    标签: java spring classpath classloader applicationcontext


    【解决方案1】:

    classpath*: 我认为有一个限制不适用于根文件夹下的文件。尝试将文件移动到文件夹下 - 例如 spring/application-a.xmlspring/application-b.xml。然后你可以有一个路径classpath*:/spring/application-*.xml

    【讨论】:

    • 这简直太疯狂了,如果 xml 文件位于 jar 内的“spring”之类的子目录中,它就可以工作。非常感谢
    猜你喜欢
    • 2014-11-20
    • 1970-01-01
    • 2013-06-18
    • 2021-05-03
    • 2020-01-09
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    相关资源
    最近更新 更多