Using env variable in Spring Boot's application.properties
You can use environment variables in your application.properties
file by using the spring.profiles.active
property.
For example, you can set the spring.profiles.active
property to prod
in your environment, and then create a separate application-prod.properties
file that contains the properties you want to override when the prod
profile is active.
In the application-prod.properties
file, you can use the ${VAR_NAME}
syntax to reference an environment variable. For example:
server.port=${PORT}
This will use the value of the PORT
environment variable as the value for the server.port
property.
You can also use the spring.config.name
property to specify the name of the .properties
file that should be loaded, and the spring.config.location
property to specify the location of the file. For example:
spring.config.name=myapp
spring.config.location=file:/etc/myapp/
This will cause Spring Boot to load the myapp.properties
file from the /etc/myapp/
directory.
Note that the application.properties
file is always loaded, regardless of the values of the spring.config.name
and spring.config.location
properties.