我在我的Spring Boot应用程序中使用了.envproperties.yml个文件,需要澄清才能正确使用它们.之后,我还将使用这些文件的其他配置文件,例如.env-devproperties-dev.yml.

你能解释一下这些问题吗?

1.据我所知,这些文件是根据IntelliJ或Maven的运行/调试配置文件自动读取的.如果活动配置文件为dev,则仅读取.env-devproperties-dev.yml个文件;如果配置文件为prod,则仅读取.env-prodproperties-prod.yml个文件.如果配置文件为"dev,prod",则这两个文件都被读取.这是真的吗?

2.如果项目中只有.envproperties.yml个文件,该怎么办?那么,当 Select 配置文件时,这些文件是否总是被读取?

3.在通过Maven运行/调试app时,我可以通过为以下命令指定文件名从.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=******"

推荐答案

别慌!它是/must be在工作...

从简单开始:

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

  2. 只是一个演示(否/空application.properties,according to 102 we can refer to any 100/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. 建房

    ./mvnw clean test
    

    打印:

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

要通过(springot-)maven-plugin设置这些插件,我们可以:

(转到)

  1. 通过"命令行参数" (用空白,用我的胜利+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'"
    

    (输出):

    ...
    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. 通过"系统属性"(Same Git Bash):

    Syntax:

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

使(例如)在……工作

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) 更复杂,由于/有ref:

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

...

Java相关问答推荐

OpenRewriter-如何替换连锁/流畅方法调用中的方法?

Java中不同包中的类之间的配置共享

Java中后期绑定的替代概念

在模拟超类中设置非setter属性的值

Java中实现的归并排序算法给出ArrayIndexOutOfBound异常

在Spring Boot应用程序中导致";MediaTypeNotSupportdException&qot;的映像上载

Arrays.hashcode(int[])为不同的元素提供相同的散列

在Eclipse中数组的可空性

带有Health Check的Spring Boot BuildPack打破了本机映像构建过程

是否为计划任务补偿系统睡眠?

如何以编程方式保存workBench.xmi?

如何在@CsvSource中传递空格作为值

将双倍转换为百分比

何时调用密封层次 struct 的switch 中的默认情况

无法在IntStream上应用Collectors.groupingBy

HBox内部的左对齐按钮(如果重要的话,在页码内)

保持标题窗格的箭头可见,即使设置为不可折叠

java.lang.ClassCastException:com.google.firebase.FirebaseException无法转换为com.google.fire base.auth.FirebaseAuthException

如何使用我的RLE程序解决此问题

始终使用Spring Boot连接mongodb上的测试数据库