Skip to content

Using env variable in Spring Boot's application.properties

You can use environment variables in your application.properties file by referencing them with the `${VAR_NAME}` syntax.

For example, you can set the spring.profiles.active property to prod to activate a separate application-prod.properties file that contains the properties you want to override when the prod profile is active.

In your properties file, you can use the `${VAR_NAME}` syntax to reference an environment variable. For example:


properties
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:


properties
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 specifying spring.config.name and spring.config.location overrides the default application.properties file and its standard locations.

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.