弹簧内部看不到jar中的文件 [英] File inside jar is not visible for spring

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

问题描述

全部

我在里面创建了一个包含以下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




  • cla sses从jar内部加载

  • spring,其他依赖项从分离的jar中加载

  • 加载spring上下文(new classPathXmlApplicationContext(spring- context / applicationContext.xml))

  • my.properties被加载到PropertyPlaceholderConfigurer(classpath:my.properties)

  • 如果我把我的.config文件在文件系统之外,并将资源url更改为'file:',一切似乎都没问题......

    • classes are loaded the from inside the jar
    • spring and other dependencies are loaded from separated jars
    • spring context is loaded (new ClassPathXmlApplicationContext("spring-context/applicationContext.xml"))
    • my.properties is loaded into PropertyPlaceholderConfigurer ("classpath:my.properties")
    • if I put my .config file outside the file system, and change the resource url to 'file:', everything seems to be fine...
    • 任何提示?

      推荐答案

      如果你的spring-context.xml和my.config文件在不同的jar中,那你就需要使用 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?

      更多信息 here

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

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

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

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