我正在尝试在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
属性中指定类路径(包含两个变体),但它总是给出相同的结果。< / p>
答案 0 :(得分:11)
假设您有两个文件,一个用于生产的本地文件
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource(value = "${ws.properties}", ignoreResourceNotFound = true)
})
在tomcat或jar文件中,传递此参数
-Dws.properties=file:/path-to.properties
我在setenv.sh中添加了这个
APPLICATION_OPTS =“ - Dlog4j.configurationFile = file:$ PATH / log4j2.xml -Dlog4j.debug = true -Dapplication.properties = file:$ PATH / application.properties
这仅适用于Spring 4
答案 1 :(得分:3)
使用变量(系统或环境)来获取文件的值,您可以像这样引用您的文件:
@PropertySource("file:${MY_PATH}/application.properties")
答案 2 :(得分:1)
我的环境是:
操作系统:Windows |容器:Tomcat | Java:7 |春天:4.2.4 | Springboot 1.3.1 |行家
步骤1 a(战争):
将文件外化属性文件添加到JVM系统属性。
正在运行tomcat;我是通过在<TOMCAT_HOME>/bin/setenv.bat
set CATALINA_OPTS=%CATALINA_OPTS% -Dexternal.app.properties=file:<PATH_TO_EXTERNAL_FILE>\application-prod.properties
步骤1 b(jar):
如果您使用jar运行,请选择
-Dexternal.app.properties=file:<PATH_TO_EXTERNAL_FILE>\application-prod.properties
请注意在行的开头使用 file:。
第2步:在我的应用程序启动类中,我使用了注释@PropertySource
来加载特定的环境应用程序属性。
@SpringBootApplication
@PropertySources({
@PropertySource(value = "${external.app.properties.file}", ignoreResourceNotFound = true),
@PropertySource(value = "classpath:application.properties")
})
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
第3步:
在项目中使用外化属性
external / file / path / application-prod.properties
spring.datasource.url.ext = LT; PRODUCTION_DATASOURCE&gt;
/src/main/resources/application.properties
spring.datasource.url = $ {spring.datasource.url.ext}
希望这有助于其他人遇到同样的问题。
答案 3 :(得分:0)
尝试提供文件的完整路径:
@PropertySource('file:c:/.../stc.properties')
答案 4 :(得分:0)
你可以在运行jar时使用--spring.config.location = file:/ somepath参数,你可以在其中指定配置文件的路径(可以是相对的)。
更多信息in docs