【发布时间】:2015-07-16 17:43:28
【问题描述】:
我正在尝试在 Jar 中使用 Spring 框架的 @PropertySource 注释从 jar 外部加载属性文件,但找不到该文件。
我需要将属性文件放在 Jar 的外部,以便可以对其进行编辑。我不知道文件的确切位置,我想我可以将它放在类路径中的任何位置。
我在 Config 类上使用了以下注释。
@PropertySource('classpath:stc.properties')
并把stc.properties与创建的Jar文件放在同一目录下。我尝试在java 命令中明确指定类路径,但仍然找不到文件:
java -cp . -jar stc.jar
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.example.stc.Config; nested exception is java.io.FileNotFoundException: class path resource [stc.properties] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:162)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:299)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
[...]
等等
我也尝试使用 ./ 作为类路径,并尝试在 jar 清单的 Class-Path 属性中指定类路径(具有两个变体),但它总是给出相同的结果。
【问题讨论】:
标签: java spring jar classpath properties-file