我试图在 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
属性中指定类路径(具有两种变体),但它总是给出相同的结果。
原文由 brianmearns 发布,翻译遵循 CC BY-SA 4.0 许可协议
假设您有两个文件,一个用于本地,一个用于生产
在tomcat或你的jar文件中,传递这个参数
我在 setenv.sh 中添加了这个
APPLICATION_OPTS=“-Dlog4j.configurationFile=file:\(PATH/log4j2.xml -Dlog4j.debug=true -Dapplication.properties=file:\)PATH/application.properties
这仅适用于 Spring 4