Spring Boot is a popular framework for building Java-based web applications. One of its key features is the ability to easily configure the application using a variety of methods, including command line arguments. In this blog post, we will take a look at how to assign a configuration location from the command line in Spring Boot.
Using --spring.config.location
command line arguments
When starting a Spring Boot application, you can use the --spring.config.name
and --spring.config.location
command line arguments to specify the location of the application’s configuration files. The --spring.config.name
argument is used to specify the name of the configuration file, while the --spring.config.location
argument is used to specify the location of the file.
For example, if you have a configuration file named application.properties
located in the config
directory of your application, you can start the application using the following command:
java -jar myapp.jar --spring.config.name=application --spring.config.location=file:config/
Using Environment Variables or System Properties
You can also specify a configuration location using environment variables or system properties. To specify a configuration location using an environment variable, you can use the spring.config.name
and spring.config.location
environment variables. To specify a configuration location using a system property, you can use the -Dspring.config.name
and -Dspring.config.location
JVM options.
For example, you can start the application using the following command:
export SPRING_CONFIG_NAME=application
export SPRING_CONFIG_LOCATION=file:config/
java -jar myapp.jar
This way, when you start your application, it will automatically look for the configuration file in the specified location. Keep in mind that if you specify the location on the command line, it will take precedence over the location specified in the system properties.
Another benefit of using system properties is that it allows you to specify the configuration location in a more dynamic way, for example, you can use a script to set the configuration location based on the environment or other variables, this can be useful in situations where you are running multiple instances of your application in different environments, and you want to have different configuration files for each environment.
Using spring.config.additional-location
command line arguments
It’s also possible to use spring.config.additional-location
to specify additional locations to search for configuration files, this allows to have multiple configuration files in different locations and Spring Boot will merge them all.
java -jar myapp.jar --spring.config.name=application --spring.config.location=file:config/ --spring.config.additional-location=file:config/dev/
Using application.properties
Another way of specifying the configuration location is by using the spring.config.name
and spring.config.location
properties in the application.properties file. This can be useful if you want to set the configuration location as a default for all instances of your application, without having to specify it on the command line every time.
For example, you can include the following in your application.properties file:
spring.config.name=application
spring.config.location=file:config/
This way, when you start your application, it will automatically look for the configuration file in the specified location. However, keep in mind that if you specify the location on the command line, it will take precedence over the location specified in the application.properties file.
Using application.yml file
Another benefit of using the application.properties file is that you can also specify the configuration location using YAML format, where you can use spring.config.name
and spring.config.location
properties in application.yml
spring:
config:
name: application
location: file:config/
It’s worth noting that you can also use the spring.config.additional-location
property in application.properties or application.yml to specify additional locations to search for configuration files, just like you can do it via command line.
Using Program API
It’s also possible to specify the configuration location using the spring.config.name
and spring.config.location
properties with the use of the SpringApplication.setDefaultProperties
method. This is useful if you want to specify the configuration location programmatically, for example, based on the environment or other variables.
Conclusion
In conclusion, Spring Boot provides several options for specifying the configuration location from the command line, including command line arguments, environment variables, system properties, application.properties and application.yml file, and programmatically through the SpringApplication.setDefaultProperties
method. This makes it easy to configure your application for different environments and use cases.