0

So I have been trying to figure out how to use environment-variables in either application.properties or application.yaml. Since this works in countless frameworks (such as Laravel for example), I assumed this would also work in Spring Boot. Now as I have found out, this is not the case.

I saw a post about Spring Environment API, that started that the environment-variable SPRING_DATASOURCE_URL would be equivalent to setting spring.datasource.url inside my application. However, this also didn't work for me.

Is there any quick way that allows using variables that are declared inside a .env file?

I'd like to use it like this inside the application.properties if possible. spring.datasource.url=jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}

6
  • It works for environment or any other variables but there is no support for .env files as those aren't exposed as environment variables afaik. Commented Nov 30, 2020 at 18:40
  • Do you have an alternative solution for this? For example, I want to run it locally by running with configuration file, but when using it with docker, I want to set the properties via. OS Environment variables. Commented Nov 30, 2020 at 18:42
  • you can have multiple profiles, set variable values as different for each profile and load them during startup Commented Nov 30, 2020 at 18:43
  • Exposing regular environemt variables works out-of-the-box. It is only that .env file that isn't supported. A docker environment variable just works. Commented Nov 30, 2020 at 18:43
  • So if I want it to work with a pure application.properties file in dev and env-variables in prod, I can just create a appilaction-dev.properties with absolute values and a application-prod.properties that uses the ${VAR_NAME} syntax? Commented Nov 30, 2020 at 18:49

2 Answers 2

1

.env is way of Python. If you use spring cloud, you can read env variable from configServer then inject them into application.properties.

  1. add some dependency into pom.xml

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
  1. define a yml to locate config-server. for example, called myBootstrap.yml
spring:
  cloud:
    config:
      fail-fast: true
      uri: http://[config-server-git]
      name: cc
      profile: config
      label: maste
  1. define a file named cc-config.properties and push it into config-server git. The env variables are written in this properties.

  2. use below way to run application jar

java -jar [your-application-jar] --spring.cloud.bootstrap.location=myBootstrap.yml
Sign up to request clarification or add additional context in comments.

Comments

0

The way I start Java Spring Boot applications from within nvim on an Ubuntu Linux machine in a terminal is by using the trick Dat Tran posted recently:

set -a; . /path/to/.env; set +a; mvn spring-boot:run

set -a; and set +a are thereby built-in shell/bash commands where the -a option is documented as:

Each variable or function that is created or modified is given the export attribute and marked for export to the environment of subsequent commands.

where the +a option basically unsets the -a option and allowing stuff that comes afterwards to access the environment variables created during the -a case. This allows Spring Boot basically to pick up environment variables and replace their placeholders in application.yaml or application.properties files.

If you want to pass additional configuration parameters like adding a debugger or opening certain modules you need to also append the following configuration, as explained i.e. here, to the command string from above:

... -Dspring-boot.run.jvmArguments="--add-opens java.base/java.utils=ALL-UNNAMED -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

This will instruct Maven to pass along the configuration options to the Spring app.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.