Reputation: 2235
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?
Upvotes: 1
Views: 15237
Reputation: 1216
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
}
Upvotes: 0
Reputation: 2749
When you run your spring boot jar, pass following argument with your location of the properties file.
java -jar myproject.jar --spring.config.location=classpath:/myconfig.properties
Upvotes: 0
Reputation: 123
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).
Upvotes: 1
Reputation: 3131
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")
Upvotes: 0
Reputation: 248
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
Upvotes: 2