读取基于Spring Boot配置文件中的配置文件`.env`和`properties.yml`。

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

Reading .env and properties.yml files based on profile in Spring Boot?

问题

  1. 根据我所知,这些文件会根据IntelliJ或Maven的运行/调试配置文件自动读取。如果活动配置文件是"dev",则只会读取.env-devproperties-dev.yml文件;如果配置文件是"prod",则只会读取.env-prodproperties-prod.yml文件。如果配置文件是"dev,prod",则两种文件都会被读取。这样是否正确?

  2. 如果项目中只有.envproperties.yml文件,那么当选择配置文件时,这些文件是否会被始终读取?

  3. 当通过Maven运行/调试应用程序时,是否可以通过以下命令来读取.env文件中的环境变量?

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug" 
-Dspring-boot.run.profiles=dev -Dspring-boot.run.arguments="DB_NAME=employee_db 
DB_USERNAME=postgres DB_PASSWORD=******"
英文:

I am using .env and properties.yml files in my Spring Boot apps and need to be clarified for using them properly. After that, I will also use the other profiles of these files e.g. .env-dev and properties-dev.yml.

Could you please explain these issues?

1. As far as I know, these files are automatically read based on the Run/Debug profile of Intellij or maven. If the active profile is dev, only .env-dev and properties-dev.yml files are read, if the profile is prod, only .env-prod and properties-prod.yml files are read. If the profile "dev,prod", then both of these files are read. Is that true?

2. What if there are only .env and properties.yml files in the project. Then, are these files always read when profile is selected or not?

3. Can I read environment variables from .env file when running/debugging app via Maven by giving file name to the following command?

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug" 
-Dspring-boot.run.profiles=dev -Dspring-boot.run.arguments="DB_NAME=employee_db 
DB_USERNAME=postgres DB_PASSWORD=******"

答案1

得分: 0

不要惊慌!它/必须正常运行...

开始简单:

  1. 快速入门 - Maven,依赖项:web(仅限)

  2. 只是一个演示(没有/空的application.properties根据foo.bar, foo.baz,我们可以引用任何常见/自定义“属性” ;):

    package com.example.demo;
    
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    
    @SpringBootApplication
    public class DemoConfigApplication {
    
      public static void main(String[] args) {
        SpringApplication.run(DemoConfigApplication.class, args);
      }
    
      @Bean
      InitializingBean test(
          @Value("${foo.bar:undefined}") String fooBar,
          @Value("${foo.baz:undefined}") String fooBaz
      ) {
        return () -> {
          System.err.format("foo.bar: %s%n", fooBar);
          System.err.format("foo.baz: %s%n", fooBaz);
        };
      }
    
    }
    
  3. 构建

    ./mvnw clean test
    

    输出:

    ...
    foo.bar:	undefined
    foo.baz:	undefined
    ...
    

要通过(Spring Boot)Maven插件设置这些,我们可以:

((Git)Ba)sh

  1. 通过“命令行参数”
    (带空格,使用我的win+gitbash:

    GNU bash, version 5.2.12(1)-release (x86_64-pc-msys)
    

    )

    语法:

    ./mvnw spring-boot:run -Dspring-boot.run.arguments="--foo.bar='Foo Bar' --foo.baz='Foo Baz'"
    

    (输出):

    ...
    2023-03-15T13:45:52.849+01:00  INFO 4196 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1106 ms
    foo.bar: Foo Bar
    foo.baz: Foo Baz
    ...
    
  2. 通过“系统属性”(相同的git bash):

    语法:

    ./mvnw spring-boot:run -Dspring-boot.run.jvmArguments="-Dfoo.bar='Foo Bar' -Dfoo.baz='Foo Baz'"
    

要使(例如)1. 在

cmd.exe

(版本10.0.19045.2673),与gitbash相同的引号:

.\mvnw.cmd spring-boot:run -Dspring-boot.run.arguments="--foo.bar='Foo Bar' --foo.baz='Foo Baz'"

PowerShell

(v7.3.3)
更复杂,因为/与参考有关:

.\mvnw.cmd spring-boot:run '-Dspring-boot.run.arguments="--foo.bar=""Foo Bar"" --foo.baz=""Foo Baz"""'

...
1: https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html
2: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.3

英文:

Don't panic! It is/must be working...

Start simple:

  1. quickstart-maven, dependencies: web (only)

  2. Just a demo (no/empty application.properties, according to foo.bar, foo.baz we can refer to any common/custom "property" ;):

    package com.example.demo;
    
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    
    @SpringBootApplication
    public class DemoConfigApplication {
    
      public static void main(String[] args) {
        SpringApplication.run(DemoConfigApplication.class, args);
      }
    
      @Bean
      InitializingBean test(
          @Value("${foo.bar:undefined}") String fooBar,
          @Value("${foo.baz:undefined}") String fooBaz
      ) {
        return () -> {
          System.err.format("foo.bar: %s%n", fooBar);
          System.err.format("foo.baz: %s%n", fooBaz);
        };
      }
    
    }
    
  3. Build

    ./mvnw clean test
    

    prints:

    ...
    foo.bar:	undefined
    foo.baz:	undefined
    ...
    

To set these via (springboot-)maven-plugin, we can:

((Git)Ba)sh

  1. Via "Command Line Arguments"
    (with blanks, with my win+gitbash:

    GNU bash, version 5.2.12(1)-release (x86_64-pc-msys)
    

    )

    Syntax:

    ./mvnw spring-boot:run -Dspring-boot.run.arguments="--foo.bar='Foo Bar' --foo.baz='Foo Baz'"
    

    (prints):

    ...
    2023-03-15T13:45:52.849+01:00  INFO 4196 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1106 ms
    foo.bar: Foo Bar
    foo.baz: Foo Baz
    ...
    
  2. Via "System Properties" (same git bash):

    Syntax:

    ./mvnw spring-boot:run -Dspring-boot.run.jvmArguments="-Dfoo.bar='Foo Bar' -Dfoo.baz='Foo Baz'"
    

To make (e.g.) 1. work in

cmd.exe

(Version 10.0.19045.2673), same quoting as gitbash:

.\mvnw.cmd spring-boot:run -Dspring-boot.run.arguments="--foo.bar='Foo Bar' --foo.baz='Foo Baz'"

PowerShell

(v7.3.3)
More complicated, due to/with ref:

.\mvnw.cmd spring-boot:run '-Dspring-boot.run.arguments="--foo.bar=""Foo Bar"" --foo.baz=""Foo Baz"""'

...

huangapple
  • 本文由 发表于 2023年3月15日 17:34:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75742838.html
匿名

发表评论

匿名网友

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

确定