|
| 1 | +package com.stubbornjava.examples.common; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.concurrent.TimeUnit; |
| 5 | + |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +import com.typesafe.config.Config; |
| 10 | +import com.typesafe.config.ConfigFactory; |
| 11 | + |
| 12 | + |
| 13 | +public class TypesafeConfigExamples { |
| 14 | + private static final Logger log = LoggerFactory.getLogger("TypesafeConfigExamples"); |
| 15 | + |
| 16 | + public static void main(String[] args) { |
| 17 | + // {{start:configs}} |
| 18 | + Config defaultConfig = ConfigFactory.parseResources("defaults.conf"); |
| 19 | + |
| 20 | + Config fallbackConfig = ConfigFactory.parseResources("overrides.conf") |
| 21 | + .withFallback(defaultConfig) |
| 22 | + .resolve(); |
| 23 | + // {{end:configs}} |
| 24 | + |
| 25 | + // {{start:text}} |
| 26 | + log.info("name: {}", defaultConfig.getString("conf.name")); |
| 27 | + log.info("name: {}", fallbackConfig.getString("conf.name")); |
| 28 | + log.info("title: {}", defaultConfig.getString("conf.title")); |
| 29 | + log.info("title: {}", fallbackConfig.getString("conf.title")); |
| 30 | + // {{end:text}} |
| 31 | + |
| 32 | + // {{start:resolved}} |
| 33 | + log.info("combined: {}", fallbackConfig.getString("conf.combined")); |
| 34 | + // {{end:resolved}} |
| 35 | + |
| 36 | + // {{start:durations}} |
| 37 | + log.info("redis.ttl minutes: {}", fallbackConfig.getDuration("redis.ttl", TimeUnit.MINUTES)); |
| 38 | + log.info("redis.ttl seconds: {}", fallbackConfig.getDuration("redis.ttl", TimeUnit.SECONDS)); |
| 39 | + // {{end:durations}} |
| 40 | + |
| 41 | + // {{start:memorySize}} |
| 42 | + // Any path in the configuration can be treated as a separate Config object. |
| 43 | + Config uploadService = fallbackConfig.getConfig("uploadService"); |
| 44 | + log.info("maxChunkSize bytes: {}", uploadService.getMemorySize("maxChunkSize").toBytes()); |
| 45 | + log.info("maxFileSize bytes: {}", uploadService.getMemorySize("maxFileSize").toBytes()); |
| 46 | + // {{end:memorySize}} |
| 47 | + |
| 48 | + // {{start:whitelist}} |
| 49 | + List<Integer> whiteList = fallbackConfig.getIntList("conf.nested.whitelistIds"); |
| 50 | + log.info("whitelist: {}", whiteList); |
| 51 | + List<String> whiteListStrings = fallbackConfig.getStringList("conf.nested.whitelistIds"); |
| 52 | + log.info("whitelist as Strings: {}", whiteListStrings); |
| 53 | + // {{end:whitelist}} |
| 54 | + |
| 55 | + |
| 56 | + // {{start:booleans}} |
| 57 | + log.info("yes: {}", fallbackConfig.getBoolean("featureFlags.featureA")); |
| 58 | + log.info("true: {}", fallbackConfig.getBoolean("featureFlags.featureB")); |
| 59 | + // {{end:booleans}} |
| 60 | + } |
| 61 | +} |
0 commit comments