jar 中的文件对于 spring 不可见 [英] File inside jar is not visible for spring

查看:23
本文介绍了jar 中的文件对于 spring 不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我创建了一个包含以下 MANIFEST.MF 的 jar 文件:

I created a jar file with the following MANIFEST.MF inside:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_25-b06 (Sun Microsystems Inc.)
Main-Class: my.Main
Class-Path: . lib/spring-core-3.2.0.M2.jar lib/spring-beans-3.2.0.M2.jar

在它的根目录中有一个名为 my.config 的文件,它在我的 spring-context.xml 中被引用,如下所示:

In its root there is a file called my.config which is referenced in my spring-context.xml like this:

<bean id="..." class="...">
    <property name="resource" value="classpath:my.config" />
</bean>

如果我运行 jar,除了加载特定文件外,一切看起来都很好:

If I run the jar, everything looks fine escept the loading of that specific file:

Caused by: java.io.FileNotFoundException: class path resource [my.config] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/D:/work/my.jar!/my.config
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:205)
    at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
    at eu.stepman.server.configuration.BeanConfigurationFactoryBean.getObject(BeanConfigurationFactoryBean.java:32)
    at eu.stepman.server.configuration.BeanConfigurationFactoryBean.getObject(BeanConfigurationFactoryBean.java:1)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    ... 22 more

  • 从 jar 内部加载类
  • spring 和其他依赖项是从分离的 jars 中加载的
  • spring 上下文已加载 (new ClassPathXmlApplicationContext("spring-context/applicationContext.xml"))
  • my.properties 被加载到 PropertyPlaceholderConfigurer ("classpath:my.properties")
  • 如果我将我的 .config 文件放在文件系统之外,并将资源 url 更改为file:",一切似乎都很好...
  • 有什么建议吗?

    推荐答案

    如果你的 spring-context.xml 和 my.config 文件在不同的 jars 中,那么你将需要使用 classpath*:my.config?

    If your spring-context.xml and my.config files are in different jars then you will need to use classpath*:my.config?

    更多信息 这里

    另外,从 jar 文件中加载时,请确保您使用的是 resource.getInputStream() 而不是 resource.getFile().

    Also, make sure you are using resource.getInputStream() not resource.getFile() when loading from inside a jar file.

    这篇关于jar 中的文件对于 spring 不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆