gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:56:27 25 4
gpt4 key购买 nike

我有一个应用程序使用 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:

关于java - 无法从 WAR 应用程序中的 JAR 文件加载 BeanIO 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34066926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com