Spring Boot - Configuration
There are many ways to set configuration to a Spring Boot application.
What is Property in Spring Boot
Properties play an important role in almost all applications, and may originate from a variety of sources: properties files, JVM system properties, system environment variables, JNDI, servlet context parameters, ad-hoc Properties objects, Maps, and so on.
Environment is an interface representing the environment in which the current application is running. Spring Boot stores properties in an Environment bean.
Command Line Properties
We can set properties from the command line. Spring Boot will add them to Spring Environment. Command line properties take precedence over the other property sources.
1 | java -jar app.jar --server.port=8000 |
you can ualso use system properties.
1 | java -Dserver.port="8000" -jar app.jar |
application.properties File
The default Property File is application.properties in src/main/resources folder.
application.properties example
1 | spring.datasource.url=jdbc:mysql://localhost:3306/persondb |
YAML based configuration is also supported. so the same config in .yml format would be
1 | spring: |
You can get System Property value with the following system. In case when the system property is not defined, default value will be used.
1 | app.tmp=${java.io.tmpdir:/mytmp} |
You can also reference an environment variable like the system properties.
1 | app.secret=${MY_SECRET:defaultsecret} |
Common application properties
Various properties can be specified inside your application.properties file, inside your application.yml file, or as command line switches.
Spring boot defines common application properties in Appendix A. Common application properties. We can also create our own properties for our application.
Use Random Values
You can provide random values in the config files.
1 | myRand: |
Reference a Value
You can reference another property in a config file.
1 | app: |
@Value Annotation
You can inject some default value as string using @Value annotation
1 |
|
Value from application.properties file
You can also get property value from a application.properties file.
1 | import org.springframework.beans.factory.annotation.Value; |
In this example, you get myservice.url
property from the application.properties file.
You can also provide some default value for the property. Here when app.secret is not defined in application.proerties file, “mydefaultsecret” will be used as the value
1 |
|
Value from System Property
You can also get System property with the same syntax
1 |
|
You can also use SpEL to get system property too
1 |
|
Value from Environment Variable
You can also get environment variables such as HOME
, PATH
1 |
|
Bind Configuration to Java Object
use @ConfigurationProperties annotaiton to bind configuration to Java Object
1 |
|
You can also use @ConfigurationProperties annotation with @Bean annotation to bind configuration to a bean in a Config class file.
1 |
|
For more information on @ConfigurationProperties annotation, see Guide to @ConfigurationProperties in Spring Boot
Register a Properties file
@PropertySource is an annotation that provides a convenient and declarative mechanism for adding a PropertySource to Spring’s Environment. To be used in conjunction with @Configuration classes.
1 |
|
Here @PropertySource is used with @Configuration annotation, you can also use it with @SpringBootApplication annotation. You can use @Value to inject properties from property source. Or you can use Environment bean to get the property value.
1 |
|
If you have multiple config files, you can use multiple @PropertySource
annotation or use @PropertySources
to group them together.
for more info on @PropertySource
, see Spring @PropertySource example by Mkyong
Environment Bean
You can use @Value to get the property value. An alternative is to use Environment bean to get the value.
In Sprint Boot, first autowire Environment bean
1 |
|
Then we can get property value like this
1 | env.getProperty("JAVA_HOME") |
Profile specific Properties file
Applications are usually deployed to different environments. We can use add configurations for different environments. Just define “application-{environment}.properties” in src/main/resources directory.
Profile-specific properties files will override the non-profile specific properties file.
application-dev.yml
1 | spring: |
application-stage.yml
1 | spring: |
You can set active profile in the command line using program argument. in this case, system property spring.profiles.active
is set to dev
1 | java -jar app.jar --spring.profiles.active=dev |
Use environment variable to set active profile
1 | SPRING_PROFILES_ACTIVE=local gradle clean bootRun |
For maven project, you can use -D to define system property
1 | mvn spring-boot:run -Dspring-boot.run.profiles=local |
You can set default profile in application.yml file
1 | spring: |
Reference
- Spring Boot Documentation - Externalized Configuration
- Baeldung - Properties with Spring and Spring Boot
- Spring Boot application properties
Source Code: SpringBootExamples - config