Java 类路径上外部文件的 Jar 中的 @PropertySource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30079255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@PropertySource in a Jar for an external file on the classpath
提问by brianmearns
I'm trying to use the Spring framework's @PropertySource
annotation in a Jar to load a properties file from outside the jar, but it's not finding the file.
我试图@PropertySource
在 Jar 中使用 Spring 框架的注释从 jar 外部加载属性文件,但它没有找到该文件。
I need the properties file to be external to the Jar so it can be edited. I don't know the exact location where the file will be, I figured I could just have it anywhere on the classpath.
我需要将属性文件放在 Jar 的外部,以便对其进行编辑。我不知道文件所在的确切位置,我想我可以将它放在类路径中的任何位置。
I'm using the following annotation on my Config
class.
我在我的Config
班级上使用以下注释。
@PropertySource('classpath:stc.properties')
And placed stc.properties
in the same directory as the created Jar file. I tried specifying the classpath explicitly in the java
command, but it still cannot find the file:
并放置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)
[...]
Etc.
等等。
I've also tried using ./
as the classpath, and tried specifying the classpath (with both variants) in the Class-Path
attribute of the jar's manifest, but it always gives the same results.
我也尝试使用./
作为类路径,并尝试Class-Path
在 jar 清单的属性中指定类路径(带有两种变体),但它总是给出相同的结果。
回答by Iob
try giving the full path of the file:
尝试给出文件的完整路径:
@PropertySource('file:c:/.../stc.properties')
回答by Tomas Briza
回答by vsingh
Assuming you have two files, one for local one for production
假设您有两个文件,一个用于本地文件,一个用于生产
@PropertySources({
@PropertySource("classpath:application.properties"),
@PropertySource(value = "${ws.properties}", ignoreResourceNotFound = true)
})
And in tomcat or your jar file , pass on this parameter
并在 tomcat 或您的 jar 文件中,传递此参数
-Dws.properties=file:/path-to.properties
I added this in setenv.sh
我在 setenv.sh 中添加了这个
APPLICATION_OPTS="-Dlog4j.configurationFile=file:$PATH/log4j2.xml -Dlog4j.debug=true -Dapplication.properties=file:$PATH/application.properties
APPLICATION_OPTS="-Dlog4j.configurationFile=file:$PATH/log4j2.xml -Dlog4j.debug=true -Dapplication.properties=file:$PATH/application.properties
This is possible with Spring 4 only
这仅适用于 Spring 4
回答by Ithar
My environment was:
我的环境是:
OS: Windows | Container: Tomcat | Java: 7 | Spring: 4.2.4 | Springboot 1.3.1 | Maven
操作系统:Windows | 容器:Tomcat | 爪哇:7 | 春天:4.2.4 | Springboot 1.3.1 | 马文
Step 1 a (war):
步骤 1 a(战争):
Add the file externalised properties file to JVM system properties.
将文件外部化属性文件添加到 JVM 系统属性。
As am running this off tomcat; I done this by creating setenv.batin <TOMCAT_HOME>/bin/setenv.bat
当我在 tomcat 上运行它时;我通过在其中创建setenv.bat来完成此操作<TOMCAT_HOME>/bin/setenv.bat
set CATALINA_OPTS=%CATALINA_OPTS% -Dexternal.app.properties=file:<PATH_TO_EXTERNAL_FILE>\application-prod.properties
Step 1 b (jar):
步骤 1 b(罐子):
Alternative if you are running from a jar use:
如果您从 jar 运行,则使用以下替代方法:
-Dexternal.app.properties=file:<PATH_TO_EXTERNAL_FILE>\application-prod.properties
-Dexternal.app.properties=file:<PATH_TO_EXTERNAL_FILE>\application-prod.properties
Note the use of file:at the start on the line.
注意使用file:在开始就行。
Step 2: In my application startup class I used annotation @PropertySource
to load the specific environment application properties.
第 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);
}
}
Step 3:
第 3 步:
Using externalised properties in project
在项目中使用外化属性
external/file/path/application-prod.properties
外部/文件/路径/应用程序-prod.properties
spring.datasource.url.ext=< PRODUCTION_DATASOURCE >
spring.datasource.url.ext=<生产数据源>
/src/main/resources/application.properties
/src/main/resources/application.properties
spring.datasource.url=${spring.datasource.url.ext}
spring.datasource.url=${spring.datasource.url.ext}
Hope this helps other having the same problem.
希望这可以帮助其他有同样问题的人。
回答by Hammad Dar
Use a variable (System or Environment) to have the value of the file and you could refer your file like this:
使用变量(系统或环境)来获取文件的值,您可以像这样引用您的文件:
@PropertySource("file:${MY_PATH}/application.properties")