Spring Boot : 如何使用@PropertySource 指向jar 外的文本文件?
Spring Boot : how to use @PropertySource to point to a text file outside the jar?
我正在开发一个独立的 Swing/Spring 引导应用程序,它将部署在许多不同的环境中。出于这个原因,我需要 Spring 加载一个外部配置文件,用户将在其中输入数据库凭据。当我用 Netbeans 运行 注释我的配置 class 时一切正常:
@PropertySource("classpath:config.properties")
在这种情况下,文本文件是从 src/main/resources 文件夹中提取的,应用程序可以使用凭据启动。我的问题涉及最终将部署的打包 JAR 文件。由于 class 路径位于 JAR 内部,用户如何才能编辑其内容?
有没有办法让@PropertySource 指向打包的 JAR 外部,例如使用我完全愿意添加的环境变量作为应用程序运行的要求?
如这里所述,您可以使用 yml 获取外部 属性:
@PropertySource("file:${application_home}config.properties")//或指定yaml文件
使用这个:
@PropertySource("file:/path/to/file")
${...} placeholders will be resolved against any/all property sources
already registered with the Environment
允许这样的事情:
@PropertySource("file:${my_var}path/to/file")
您可以将配置文件作为类路径的一部分(在 jar 中),并将另一个配置文件作为类路径的一部分,然后覆盖打包配置中的默认值。您可以从类路径外部加载外部文件,方法是使用文件:URI 而不是类路径:
@PropertySource("file:/config.properties")
但是:您不必自己使用@PropertySource 注入配置文件:Spring 引导自动从不同位置加载配置文件:
SpringApplication will load properties from application.properties files in the following locations and add them to the Spring
Environment:
A /config subdirectory of the current directory.
The current directory
A classpath /config package
The classpath root
The list is ordered by
precedence (properties defined in locations higher in the list
override those defined in lower locations).
当您 运行 您的 spring 引导 jar 时,将以下参数与您的属性文件位置一起传递。
java -jar myproject.jar --spring.config.location=classpath:/myconfig.properties
这将像魔术一样工作,并且在 windows 和 linux 上都能完美运行。
这是用于开发的示例配置 class 如果 spring 活动配置文件是 dev
,它只读取开发属性文件
@Configuration
@Profile("dev")
@PropertySource("file:///${user.home}/path/application-dev.properties")
public class DevelopmentConfig {
// Your logic goes here
}
我正在开发一个独立的 Swing/Spring 引导应用程序,它将部署在许多不同的环境中。出于这个原因,我需要 Spring 加载一个外部配置文件,用户将在其中输入数据库凭据。当我用 Netbeans 运行 注释我的配置 class 时一切正常:
@PropertySource("classpath:config.properties")
在这种情况下,文本文件是从 src/main/resources 文件夹中提取的,应用程序可以使用凭据启动。我的问题涉及最终将部署的打包 JAR 文件。由于 class 路径位于 JAR 内部,用户如何才能编辑其内容?
有没有办法让@PropertySource 指向打包的 JAR 外部,例如使用我完全愿意添加的环境变量作为应用程序运行的要求?
如这里所述,您可以使用 yml 获取外部 属性:
@PropertySource("file:${application_home}config.properties")//或指定yaml文件
使用这个:
@PropertySource("file:/path/to/file")
${...} placeholders will be resolved against any/all property sources already registered with the Environment
允许这样的事情:
@PropertySource("file:${my_var}path/to/file")
您可以将配置文件作为类路径的一部分(在 jar 中),并将另一个配置文件作为类路径的一部分,然后覆盖打包配置中的默认值。您可以从类路径外部加载外部文件,方法是使用文件:URI 而不是类路径:
@PropertySource("file:/config.properties")
但是:您不必自己使用@PropertySource 注入配置文件:Spring 引导自动从不同位置加载配置文件:
SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
A /config subdirectory of the current directory.
The current directory
A classpath /config package
The classpath root
The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).
当您 运行 您的 spring 引导 jar 时,将以下参数与您的属性文件位置一起传递。
java -jar myproject.jar --spring.config.location=classpath:/myconfig.properties
这将像魔术一样工作,并且在 windows 和 linux 上都能完美运行。 这是用于开发的示例配置 class 如果 spring 活动配置文件是 dev
,它只读取开发属性文件@Configuration
@Profile("dev")
@PropertySource("file:///${user.home}/path/application-dev.properties")
public class DevelopmentConfig {
// Your logic goes here
}