|
1 | 1 | # Spring Boot Overriding Environment Variables Guide
|
2 |
| -This is an example of overriding the application.yml file with the OS environment variables. `Single` or `Array` environment variables can be overridden. |
| 2 | + |
| 3 | + |
| 4 | +This is an example of overriding the application.yml file with the OS environment variables. |
| 5 | +`Single` or `Array` environment variables can be overridden. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +We talk about many ways to override environment variables in a Spring Boot application. Not auto-configuration :( |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Understanding Environment Variables |
| 14 | +* Your Spring Boot application has a `application.yml` or `application.properties` file. |
| 15 | +* The application properties are converted to environment variables when you run the Spring Boot application. |
| 16 | +* The environment variables are key-value pairs that are set in the operating system. |
| 17 | +* They are used to configure applications and services. |
| 18 | +* Many ways to override environment variables in a Spring Boot application. |
| 19 | + |
| 20 | +--- |
3 | 21 |
|
4 | 22 | ## Overview
|
5 | 23 | 1. This guide will show you how to override environment variables in a Spring Boot application.
|
6 |
| -2. Single and multiple environment variables will be overridden. |
| 24 | +2. Single and array environment variables will be overridden. |
7 | 25 | 3. When you run a Spring Boot application, the application properties are converted to environment variables.
|
8 | 26 | 4. You can override environment variables using the `-D` flag when java -jar is used.
|
9 | 27 | 5. You can override environment variables using the `-e` flag when running a Spring Boot application in a Docker container.
|
10 | 28 | 6. You can override environment variables using ConfigMaps and Secrets when running a Spring Boot application in a Kubernetes cluster.
|
11 | 29 | 7. You can override environment variables using the `application.yml` file from git repository with spring cloud config server.
|
| 30 | + |
| 31 | +---- |
| 32 | + |
| 33 | +## Prerequisites |
| 34 | +* JDK 17 |
| 35 | +* Maven 3.9x |
| 36 | +* Spring Boot 3x |
| 37 | + |
12 | 38 | ---
|
13 | 39 |
|
14 |
| -## Our Use Case |
| 40 | +## Getting Started |
| 41 | +* Clone the repository |
| 42 | +```shell |
| 43 | +git clone https://github.com/cevheri/spring-boot-overriding-environment-variables.git |
| 44 | +``` |
| 45 | + |
| 46 | +* Change directory |
| 47 | +```shell |
| 48 | +cd spring-boot-overriding-environment-variables |
| 49 | +``` |
| 50 | + |
| 51 | +* Run the Spring Boot application |
| 52 | +```shell |
| 53 | +mvn spring-boot:run |
| 54 | +``` |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | + |
| 59 | +## Our Use Cases |
| 60 | + |
15 | 61 | 4 and 5 are the most common use cases for overriding environment variables in a Spring Boot application. Maybe next time we can talk about the Kubernetes and Spring Cloud Config Server use cases.
|
16 | 62 |
|
17 | 63 | ---
|
@@ -75,25 +121,46 @@ Run the Spring Boot application using the `java -jar` command.
|
75 | 121 | ```shell
|
76 | 122 | java -jar target/soe.jar
|
77 | 123 | ```
|
78 |
| - |
| 124 | + |
79 | 125 | ---
|
80 | 126 |
|
81 | 127 | #### Override Environment Variables
|
82 | 128 | ```shell
|
83 | 129 | java -jar '-Dapp-properties.name=overridden-name-cevheri' '-Dapp-roles[0]=overridden-role-0-cevheri' '-Dapp-paths[0].roles[4]=overridden-path-0-role-0-cevheri' target/soe.jar
|
84 | 130 | ```
|
| 131 | + |
| 132 | +--- |
| 133 | + |
85 | 134 | ### Docker
|
86 | 135 | #### Simple
|
87 | 136 | ```shell
|
88 | 137 | docker run -p 8080:8080 soe:latest
|
89 | 138 | ```
|
90 |
| - |
| 139 | + |
91 | 140 | #### Override Environment Variables
|
92 | 141 | ```shell
|
93 | 142 | docker run -p 8080:8080 -e APP_PROPERTIES_NAME=overridden-name-cevheri-docker -e APP_ROLES_0=overridden-role-0-cevheri-docker -e APP_PATHS_0_ROLES_4=overridden-path-0-role-0-cevheri-docker soe:latest
|
94 | 143 | ```
|
| 144 | + |
95 | 145 |
|
96 | 146 |
|
| 147 | +## Test |
| 148 | + |
| 149 | +```shell |
| 150 | +mvn test |
| 151 | +``` |
| 152 | + |
| 153 | +--- |
| 154 | + |
| 155 | +## Conclusion |
| 156 | +* This guide showed you how to override environment variables in a Spring Boot application. |
| 157 | +* Single and multiple(array) environment variables were overridden. |
| 158 | +* When you run a Spring Boot application, the application properties are converted to environment variables. |
| 159 | +* You can override environment variables using the `-D` flag when java -jar is used. |
| 160 | +* You can override environment variables using the `-e` flag when running a Spring Boot application in a Docker container. |
| 161 | + |
| 162 | +--- |
| 163 | + |
97 | 164 | ### Reference Documentation
|
98 | 165 |
|
99 | 166 | For further reference, please consider the following sections:
|
|
0 commit comments