我可以在 Micronaut 中添加默认配置文件吗?

Jam*_*lds 1 micronaut

使用 Spring,我可以使用以下命令包含具有非默认名称的配置文件:

public static void main(String[] args) {

/*
This allows us to add an additional configuration file to the default configuration.
By using the `spring.config.name` property, we can specify the order of property files to load from the `spring.config.location` that we specify on startup (if not specified, it assumes the default).  This also respects profiles as well.
*/
        ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(DirectconnectEdgeserviceApplication.class)
                .properties("spring.config.name:application,secrets")
                .build().run(args);
    }

Run Code Online (Sandbox Code Playgroud)

它使用 Spring 属性spring.config.name来指定配置文件将被命名为application*secrets*.

我可能可以导入io.micronaut.spring.context并使用 MicronautApplicationContext,但我想知道是否有办法在普通 Micronaut 中执行此操作?

Jef*_*own 5

我可以在 Micronaut 中添加默认配置文件吗?

是的。有多种方法可以做到这一点。一种常见的方法是使用micronaut.config.filesJVM 系统属性来指向文件。

java -Dmicronaut.config.files="/path/to/someConfig.yml" myapp.jar
Run Code Online (Sandbox Code Playgroud)

您还可以设置相应的操作系统环境变量:

export MICRONAUT_CONFIG_FILES=somefile.yml
Run Code Online (Sandbox Code Playgroud)

更多信息请访问https://docs.micronaut.io/2.1.1/guide/index.html#propertySource