简体   繁体   中英

Spring Boot : how to use @PropertySource to point to a text file outside the jar?

I am working on a standalone Swing/Spring Boot application which will be deployed in a number of different environments. For that reason, i need Spring to load an external config file where the user will enter the database credentials. Everything works fine when i annotate my configuration class with and when i run it from Netbeans:

@PropertySource("classpath:config.properties")

In this case, the text file is fetched from the src/main/resources folder and the application can use the credentials to launch. My question concerns the packaged JAR file that will ultimately be deployed. Since the classpath is INSIDE the JAR, how are the users expected to be able to edit its content?

Is there a way for @PropertySource to point outside the packaged JAR, for example using an environment variable which I am totally willing to add as a requirement for the app to work?

As mentioned here you can use yml to get the external property:

Spring boot external configuration of property file

@PropertySource("file:${application_home}config.properties")//or specify yaml file

Use this:

@PropertySource("file:/path/to/file")

From the javadocs for PropertySource

${...} placeholders will be resolved against any/all property sources already registered with the Environment

Allowing for things like:

@PropertySource("file:${my_var}path/to/file")

You can have your configuration file as part of your classpath (inside the jar), and another one outside the classpath, which then overrides the default values in your packaged configuration. You could load an external file from outside the classpath, by using a file: URI instead of classpath:

@PropertySource("file:/config.properties")

However: You do not have to inject the configuration file yourself with @PropertySource: Spring boot automatically loads configuration files from different locations:

This is documented here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

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 boot jar时,将以下参数与属性文件的位置一起传递。

java -jar myproject.jar --spring.config.location=classpath:/myconfig.properties 

This will work like magic and it works perfectly on both windows and linux. This is a sample configuration class for development which reads only dev properties file if spring active profile is dev

@Configuration
@Profile("dev")
@PropertySource("file:///${user.home}/path/application-dev.properties")
public class DevelopmentConfig {
    // Your logic goes here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM