How to Use Environment Variables in Spring Boot's application.properties

Question

How can we dynamically configure database connection details in Spring Boot's application.properties using environment variables?

export OPENSHIFT_MYSQL_DB_HOST="jdbc:mysql://localhost"
export OPENSHIFT_MYSQL_DB_PORT="3306"
export OPENSHIFT_MYSQL_DB_USERNAME="root"
export OPENSHIFT_MYSQL_DB_PASSWORD="123asd"

Answer

In Spring Boot, you can easily use environment variables to configure your application settings, including database connection properties, by referencing them in the application.properties file. This flexibility is particularly useful in multi-environment setups such as local development, continuous integration (like Jenkins), and production deployments (like OpenShift).

spring.datasource.url=${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/nameofDB
spring.datasource.username=${OPENSHIFT_MYSQL_DB_USERNAME}
spring.datasource.password=${OPENSHIFT_MYSQL_DB_PASSWORD}

Causes

  • Static configuration in application.properties can lead to problems when deploying across multiple environments.
  • Environment-specific styles can require different database credentials and connection URLs.

Solutions

  • Define environment variables in your system or CI/CD environments (e.g., Jenkins and OpenShift).
  • Refer to these environment variables in your application.properties file using the correct syntax.

Common Mistakes

Mistake: Incorrect use of variable syntax in application.properties (e.g., using `sqlURL` instead of the correct variable name).

Solution: Ensure to use the exact names of the environment variables. For example, use `OPENSHIFT_MYSQL_DB_HOST` instead of `sqlURL`.

Mistake: Not exporting the environment variables before launching the application or Jenkins build, causing the application to fail to read them.

Solution: Make sure to export the environment variables in the environment where your application is running.

Mistake: Using quotes around environment variables in the application.properties file.

Solution: Do not include quotes when referencing environment variables in application.properties.

Helpers

  • Spring Boot
  • environment variables
  • application.properties
  • database connection
  • MySQL
  • Jenkins
  • OpenShift

Related Questions

⦿What is a Java Servlet? A Beginner's Introduction

Learn what Java Servlets are their advantages and how they differ from other serverside programming languages like PHP and ASP.

⦿Understanding the Differences Between Static and Inner Classes in Java

Explore the key differences between static and inner classes in Java. Learn about their structure behavior and use cases to enhance your Java programming skills.

⦿Why Does the Error 'Non-Static Method Cannot Be Referenced from a Static Context' Occur?

Discover the reasons behind the nonstatic method cannot be referenced from a static context error in Java along with explanations and solutions.

⦿How to Retrieve the Current Year as an Integer in Java?

Learn how to obtain the current year as an integer in Java using the LocalDate class from the Java Time API.

⦿How to Dynamically Load JAR Files at Runtime in Java?

Learn how to load JAR files dynamically in Java using custom ClassLoader techniques with code examples and tips.

⦿Understanding the Difference Between application.yml and bootstrap.yml in Spring Boot

Learn the key differences between application.yml and bootstrap.yml in Spring Boot including their purposes and effects on application configuration.

⦿How to Encode Data in Base64 Format Using Java

Discover how to easily encode data in Base64 format using Javas builtin classes without running into common pitfalls.

⦿How to Dynamically Create an Instance of a Class and Pass Constructor Parameters in Java?

Learn how to create an object dynamically in Java using class names and pass arguments to its constructor with this detailed guide.

⦿Understanding the Difference Between Static and Non-Static Initialization Code Blocks in Java

Discover the differences between static and nonstatic initialization blocks in Java their purpose and how to use them effectively.

⦿How to Fix the 'No Serializer Found' Error When Serializing with Jackson?

Learn how to resolve the No serializer found error in Jackson when serializing objects. Stepbystep guide with code snippets included.

© Copyright 2025 - CodingTechRoom.com

close