Reading .env file parameters from application.yml in Spring Boot?

huangapple go评论69阅读模式
英文:

Reading .env file parameters from application.yml in Spring Boot?

问题

I use some sensitive data by passing as environment variables via IntelliJ run config:

spring:
...
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/${DB_NAME}
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}

However, I am wondering if it is possible to read from .env file and match these labels without passing them via IntelliJ or command line. In docker-compose it is possible using env_file parameter as far as I remember. Is there a similar thing for application properties to read and match parameters in .env file by giving its location?

英文:

I use some sensitive data by passing as environment variables via IntelliJ run config:


spring:
...
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/${DB_NAME}
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}

However, I am wondering if it is possible to read from .env file and match these labels without passing them via IntelliJ or command line. In docker-compose it is possible using env_file parameter as far as I remember. Is there a similar thing for application properties to read and match parameters in .env file by giving its location?

答案1

得分: 0

I'm assuming you put the .env file in the top of the project directory, you can add the following configuration to read variables from the .env file

...
spring:
    config:
        import: optional:file:.env[.properties]
...
英文:

I'm assuming you put the .env file in the top of the project directory, you can add the following configuration to read variables from the .env file

...
spring:
    config:
        import: optional:file:.env[.properties]
...

答案2

得分: -1

No, it's not supported. I would say, it's not a job of Spring Boot to read .env files. It's a job of your shell environment (i.e. bash, zsh) or a job of your IDE (there are corresponding plugins for Intellij).

But as I can see you do some unnecessary work by introducing your own env variables and mapping them to Spring Boot properties. It's unnecessary because the value for each Spring Boot property can be passed and recognized with a corresponding uppercased environment variable. This means that if you want to pass a value for spring.datasource.password property to Spring Boot, you can just start the app with a value in SPRING_DATASOURCE_PASSWORD environment variable.

Also, I do not see any reasons to use .env file while developing apps with Spring Boot. In your src/resources/application.yaml, just set spring.profiles.active: secrets and put your sensitive data (which you do not want to commit) into application-secrets.yaml (which is .gitignored to avoid accidental commit).

Please note that ideally you also should not keep secrets in a file when deploying your app. You should inject them as an environment variable on startup of your application's process, so it would only exist in the memory of a working process.

英文:

No, it's not supported. I would say, it's not a job of Spring Boot to read .env files.
It's a job of your shell environment (i.e. bash, zsh) or a job of your IDE (there are corresponding plugins for Intellij)

But as I can see you do some unnecessary work by introducing your own env variables and mapping them to Spring Boot properties.
It's unnecessary, because the value for each Spring Boot property can be passed and recognized with a corresponding uppercased environment variable (see this docs section). This means that if you want to pass a value for spring.datasource.password property to Spring Boot, you can just start the app with a value in SPRING_DATASOURCE_PASSWORD environment variable.

Also, I do not see any reasons to use .env file while developing apps with Spring Boot. In your src/resources/application.yaml just set spring.profiles.active: secrets and put your sensitive data (which you do not want to commit) into application-secrets.yaml (which is .gitignored to avoid accidental commit)

Please note that ideally you also should not keep secrets in a file when deploying your app. You should inject them as environment variable on startup of your application's process, so it would only exist in a memory of a working process.

huangapple
  • 本文由 发表于 2023年3月21日 02:12:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定