bri*_*rns 6 java spring jar classpath properties-file
我正在尝试@PropertySource在Jar中使用Spring框架的注释来从jar外部加载属性文件,但它没有找到该文件.
我需要将属性文件放在Jar的外部,以便进行编辑.我不知道文件的确切位置,我想我可以把它放在类路径的任何地方.
我在Config课堂上使用以下注释.
@PropertySource('classpath:stc.properties')
Run Code Online (Sandbox Code Playgroud)
并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)
[...]
Run Code Online (Sandbox Code Playgroud)
等等.
我也尝试使用./类路径,并尝试Class-Path在jar的清单的属性中指定类路径(包含两个变体),但它总是给出相同的结果.
vsi*_*ngh 12
假设您有两个文件,一个用于本地生产
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource(value = "${ws.properties}", ignoreResourceNotFound = true)
})
Run Code Online (Sandbox Code Playgroud)
在tomcat或jar文件中,传递此参数
-Dws.properties=file:/path-to.properties
Run Code Online (Sandbox Code Playgroud)
我在setenv.sh中添加了这个
APPLICATION_OPTS =" - Dlog4j.configurationFile = file:$ PATH/log4j2.xml -Dlog4j.debug = true -Dapplication.properties = file:$ PATH/application.properties
这仅适用于Spring 4
使用变量(系统或环境)获取文件的值,您可以像这样引用文件:
@PropertySource("file:${MY_PATH}/application.properties")
Run Code Online (Sandbox Code Playgroud)
小智 0
尝试给出文件的完整路径:
@PropertySource('file:c:/.../stc.properties')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18974 次 |
| 最近记录: |