Oracle Helidon配置和访问config示例

1,创建一个Helidon MP项目

mvn archetype:generate -DinteractiveMode=false \
    -DarchetypeGroupId=io.helidon.archetypes \
    -DarchetypeArtifactId=helidon-quickstart-mp \
    -DarchetypeVersion=1.0.0 \
    -DgroupId=io.helidon.examples \
    -DartifactId=helidon-config-basic \
    -Dpackage=io.helidon.examples.quickstart.mp

2,在src/main/resources下创建application.conf。application.conf的内容如下。

app {
    greeting = "Hello"
    name = "Demo"
    page-size = 20
    basic-range = [ -20, 20 ]
    storagePassphrase = "${AES=thisIsEncriptedPassphrase}"
}

logging {
    outputs {
        console {
            pattern = simple.colored
            level = INFO
        }
        file {
            pattern = verbose.colored
            level = DEBUG
            name = target/root.log
        }
    }
    level = INFO
    app.level = DEBUG
    com.oracle.prime.level = WARN
}

3,修改src/main/java/io/helidon/examples/quickstart/mp/Main.java。

在main方法体内,追加如下代码。

import java.nio.file.Path;
import java.util.List;

import io.helidon.config.Config;
import static io.helidon.config.ConfigSources.classpath;
        Config config = Config.create(classpath("application.conf"));

        int pageSize = config.get("app.page-size").asInt().get();

        boolean storageEnabled = config.get("app.storageEnabled").asBoolean().orElse(false);

        List basicRange = config.get("app.basic-range").asList(Integer.class).get();

        Path loggingOutputPath = config.get("logging.outputs.file.name").as(Path.class).get();

        System.out.println(pageSize);
        System.out.println(storageEnabled);
        System.out.println(basicRange);
        System.out.println(loggingOutputPath);

4,执行Main.java。控制台输出

20
false
[-20, 20]
target/root.log

结尾!

你可能感兴趣的:(Helidon)