{"id":19233237,"url":"https://github.com/cevheri/java-spring-boot-overriding-environment-variables","last_synced_at":"2026-04-21T09:31:20.299Z","repository":{"id":250778191,"uuid":"835289975","full_name":"cevheri/java-spring-boot-overriding-environment-variables","owner":"cevheri","description":"This is an example Java Spring Boot Application of overriding the application.yml file with the OS environment variables.  Single or Array environment variables can be overridden.","archived":false,"fork":false,"pushed_at":"2024-07-31T11:06:17.000Z","size":1100,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-23T11:42:01.682Z","etag":null,"topics":["application-properties","application-yml","docker","dockerfile","environment-variables","java","spring-boot"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cevheri.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-29T14:31:37.000Z","updated_at":"2025-01-16T12:47:05.000Z","dependencies_parsed_at":"2024-07-30T03:41:47.405Z","dependency_job_id":null,"html_url":"https://github.com/cevheri/java-spring-boot-overriding-environment-variables","commit_stats":null,"previous_names":["cevheri/java-spring-boot-overriding-environment-variables"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/cevheri/java-spring-boot-overriding-environment-variables","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cevheri%2Fjava-spring-boot-overriding-environment-variables","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cevheri%2Fjava-spring-boot-overriding-environment-variables/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cevheri%2Fjava-spring-boot-overriding-environment-variables/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cevheri%2Fjava-spring-boot-overriding-environment-variables/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cevheri","download_url":"https://codeload.github.com/cevheri/java-spring-boot-overriding-environment-variables/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cevheri%2Fjava-spring-boot-overriding-environment-variables/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32085405,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-21T06:27:27.065Z","status":"ssl_error","status_checked_at":"2026-04-21T06:27:21.250Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["application-properties","application-yml","docker","dockerfile","environment-variables","java","spring-boot"],"created_at":"2024-11-09T16:09:24.761Z","updated_at":"2026-04-21T09:31:19.781Z","avatar_url":"https://github.com/cevheri.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Java Spring Boot Overriding Environment Variables Guide\n\n\nThis is an example of overriding the application.yml file with the OS environment variables. \n`Single` or `Array` environment variables can be overridden.\n\n![img.png](images/title.png)\n\nWe talk about many ways to override environment variables in a Spring Boot application. Not autoconfiguration values :(\n\n---\n\n## Understanding Environment Variables\n* Your Spring Boot application has a `application.yml` or `application.properties` file.\n* The application properties are converted to environment variables when you run the Spring Boot application.\n* The environment variables are key-value pairs that are set in the operating system.\n* They are used to configure applications and services.\n* Many ways to override environment variables in a Spring Boot application.\n\n---\n\n## Overview\n1. This guide will show you how to override environment variables in a Spring Boot application.\n2. Single and array environment variables will be overridden.\n3. When you run a Spring Boot application, the application properties are converted to environment variables.\n4. You can override environment variables using the `-D` flag when java -jar is used.\n5. You can override environment variables using the `-e` flag when running a Spring Boot application in a Docker container.\n6. You can override environment variables using ConfigMaps and Secrets when running a Spring Boot application in a Kubernetes cluster.\n7. You can override environment variables using the `application.yml` file from git repository with spring cloud config server.\n\n----\n\n## Prerequisites\n* JDK 17\n* Maven 3.9x\n* Spring Boot 3x\n\n---\n\n## Getting Started\n* Clone the repository\n```shell\ngit clone https://github.com/cevheri/java-spring-boot-overriding-environment-variables.git\n```\n\n* Change directory\n```shell\ncd java-spring-boot-overriding-environment-variables\n```\n\n* Run the Spring Boot application\n```shell\nmvn spring-boot:run\n```\n\n---\n\n\n## Our Use Cases\n![img.png](images/uses-cases.png)\n4 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.\n\n---\n\n## Environments Naming Convention\nWhen you run a Spring Boot application, the application properties are converted to environment variable like UPPER_CASE with underscores.\n\n* application.yml file\n```yml\napp-object:\n  name: \"Spring Boot Overriding Environment Variables\"\n  version: \"0.0.1\"\n  description: \"Spring Boot Overriding Environment Variables with application.yml\"\n  author: \"Cevheri\"\n  email: \"cevheribozoglan@gmail.com\"\n```\n* OS Environment Variable\n```shell\nAPP_OBJECT_NAME=overridden-name\nAPP_OBJECT_VERSION=overridden-version\nAPP_OBJECT_DESCRIPTION=overridden-description\nAPP_OBJECT_AUTHOR=overridden-author\nAPP_OBJECT_EMAIL=overridden-email\n```\n\n---\n\n## Use Cases\nEnvironment variables are key-value pairs that are set in the operating system.\nThey are used to configure applications and services.\n\n### java -jar\nWhen you run a Spring Boot application using the `java -jar` command, you can override environment variables using the `-D` flag.\n\n\n\n### docker run\nWhen you run a Spring Boot application in a Docker container, you can override environment variables using the `-e` flag.\n    \n\n---\n\n## Build\n### Maven Build\n```shell\nmvn clean install\n```\n\n### Build Docker Image\n```shell\ndocker build -t soe:latest .\n```\n\n---\n\n## Run\nRun the Spring Boot application using the `java -jar` command. \n\n### Java\n#### Simple\n```shell\njava -jar target/soe.jar\n```\n![default-values.png](images/java-default-values.png)\n---\n\n#### Override Environment Variables\n```shell\njava -jar '-Dapp-object.name=overridden-name-cevheri' '-Dapp-arrays.roles[0]=overridden-role-0-cevheri' '-Dapp-multiple.paths[0].roles[0]=overridden-path-0-role-0-cevheri' target/soe.jar\n```\n![override-values.png](images/java-override-values.png)\n\n---\n\n### Docker\n#### Simple\n```shell\ndocker run -p 8080:8080 soe:latest\n```\n![docker-default-values.png](images/docker-default-values.png)\n#### Override Environment Variables\n```shell\ndocker run -p 8080:8080 -e APP_OBJECT_NAME=overridden-name-cevheri-docker -e APP_ARRAYS_ROLES_0=overridden-role-0-cevheri-docker -e APP_MULTIPLE_PATHS_0_ROLES_0=overridden-path-0-role-0-cevheri-docker soe:latest\n```\n![docker-override-values.png](images/docker-override-values.png)\n\n\n---\n## Using Spring Bean\n```java\n@Configuration\n@ConfigurationProperties(prefix = \"app-multiple\")\npublic class AppPaths {\n\n    private List\u003cPath\u003e paths;\n\n    public List\u003cPath\u003e getPaths() {\n        return paths;\n    }\n\n    public void setPaths(List\u003cPath\u003e paths) {\n        this.paths = paths;\n    }\n\n    public static class Path {\n        private String method;\n        private List\u003cString\u003e roles;\n\n        public String getMethod() {\n            return method;\n        }\n\n        public void setMethod(String method) {\n            this.method = method;\n        }\n\n        public List\u003cString\u003e getRoles() {\n            return roles;\n        }\n\n        public void setRoles(List\u003cString\u003e roles) {\n            this.roles = roles;\n        }\n    }\n}\n```\nYou should override whole array object like below.\n```shell\ndocker run -p 8080:8080 \\\n-e APP_MULTIPLE_PATHS_0_METHOD=NEW_POST \\\n-e APP_MULTIPLE_PATHS_0_ROLES_0=new_create \\\n-e APP_MULTIPLE_PATHS_0_ROLES_1=new_read \\\n-e APP_MULTIPLE_PATHS_0_ROLES_2=new_update \\\n-e APP_MULTIPLE_PATHS_0_ROLES_3=new_delete \\\n-e APP_MULTIPLE_PATHS_0_ROLES_4=new_admin \\\n soe:latest\n \n```\n\n---\n\n## Test\n\n```shell\nmvn test\n```\n\n---\n\n## Conclusion\n* This guide showed you how to override environment variables in a Spring Boot application. \n* Single and multiple(array) environment variables were overridden. \n* When you run a Spring Boot application, the application properties are converted to environment variables. \n* You can override environment variables using the `-D` flag when java -jar is used. \n* You can override environment variables using the `-e` flag when running a Spring Boot application in a Docker container.\n\n---\n\n### Reference Documentation\n\nFor further reference, please consider the following sections:\n* https://spring.io/guides/gs/spring-boot\n* https://docs.spring.io/spring-boot/reference/features/external-config.html\n* https://maven.apache.org/guides/index.html\n* https://docs.spring.io/spring-boot/3.3.2/maven-plugin\n* https://docs.spring.io/spring-boot/3.3.2/maven-plugin/build-image.html\n* https://docs.docker.com/compose/environment-variables/set-environment-variables/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcevheri%2Fjava-spring-boot-overriding-environment-variables","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcevheri%2Fjava-spring-boot-overriding-environment-variables","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcevheri%2Fjava-spring-boot-overriding-environment-variables/lists"}