无法从 WAR 应用程序中的 JAR 文件加载 BeanIO 映射

Unable to load BeanIO mapping from JAR file in WAR application

我有一个应用程序使用 XML 配置加载其 Spring 上下文。

上下文文件存储在 jar 的类路径中(在 META-INF/context 目录中)。

引入一个新 bean,我想从同一存档的类路径中加载一个 beanio 映射文件。 在 Eclipse 中使用 Tomcat 运行 一切都很好,但是当我将 WAR 文件部署到我们的 QA 环境时,我的应用程序无法启动。

让我走代码:

public class BeanIoParser implements Parser, Iterator<Message>, InitializingBean
{
    private StreamFactory streamFactory;

    private Resource resourceMapping; //Injected via Context

    @Override
    public void afterPropertiesSet() throws Exception
    {
        streamFactory = StreamFactory.newInstance();
        streamFactory.load(resourceMapping.getFile()); //boom here
    }
}

然后是上下文

<bean id="messagesParser" class="it.acme.BeanIoParser" scope="prototype">
    <property name="resourceMapping" value="classpath:META-INF/mappings/messages-beanio.xml" />
</bean>

然后呢?嗯...我已经通过首先解压缩 war 文件仔细检查了构建的 jar 文件。全部匹配。

我可以正确看到我的 messages-beanio.xml 文件。

但是当我启动应用程序时,我得到以下 根本原因

java.io.FileNotFoundException: class path resource [META-INF/mappings/messages-beanio.xml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%208.0/temp/11-app/WEB-INF/lib/app-3.0.0.20151105.jar!/META-INF/mappings/messages-beanio.xml

我也尝试过另一种方法,使用 classpath*(因为我们在从多个 jar 文件加载 Spring 上下文时已经遇到了这个问题)

所以如果我的 bean 声明变成

<property name="resourceMapping" value="classpath*:META-INF/mappings/messages-beanio.xml" />    

我的错误变成了

java.io.FileNotFoundException: ServletContext resource [/classpath*:META-INF/mappings/messages-beanio.xml] cannot be resolved to absolute file path - web application archive not expanded?

我该如何解决?

解决方式不同。我以编程方式实例化 ClasspathResource,路径以 META-INF 开头,而不是 classpath: