"File inside jar is not visible for spring" 的意思是:在Jar包中的文件对于Spring不可见。

10 浏览
0 Comments

"File inside jar is not visible for spring" 的意思是:在Jar包中的文件对于Spring不可见。

全部

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

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文件中被引用,如下所示:

    

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

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和其他依赖项是从不同的JAR文件中加载的
  • Spring上下文被加载(new ClassPathXmlApplicationContext(\"spring-context/applicationContext.xml\"))
  • my.properties被加载到PropertyPlaceholderConfigurer中(\"classpath:my.properties\")
  • 如果我将my.config文件放在文件系统外面,并将资源URL更改为\'file:\',似乎一切都很好......

有什么建议吗?

admin 更改状态以发布 2023年5月20日
0
0 Comments

在 spring jar 包中,我使用新的 ClassPathResource(filename).getFile(),会抛出以下异常:

无法解析为绝对文件路径,因为它不存在于文件系统中:jar

但是使用 new ClassPathResource(filename).getInputStream() 可以解决这个问题。原因是 jar 包中的配置文件并不存在于操作系统的文件树中,因此必须使用 getInputStream()

0
0 Comments

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

更多信息请查看这里

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

0