Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...njava-examples/src/main/java/com/stubbornjava/examples/common/TypesafeConfigExamples.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.stubbornjava.examples.common; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigFactory; | ||
|
||
|
||
public class TypesafeConfigExamples { | ||
private static final Logger log = LoggerFactory.getLogger("TypesafeConfigExamples"); | ||
|
||
public static void main(String[] args) { | ||
// {{start:configs}} | ||
Config defaultConfig = ConfigFactory.parseResources("defaults.conf"); | ||
|
||
Config fallbackConfig = ConfigFactory.parseResources("overrides.conf") | ||
.withFallback(defaultConfig) | ||
.resolve(); | ||
// {{end:configs}} | ||
|
||
// {{start:text}} | ||
log.info("name: {}", defaultConfig.getString("conf.name")); | ||
log.info("name: {}", fallbackConfig.getString("conf.name")); | ||
log.info("title: {}", defaultConfig.getString("conf.title")); | ||
log.info("title: {}", fallbackConfig.getString("conf.title")); | ||
// {{end:text}} | ||
|
||
// {{start:resolved}} | ||
log.info("combined: {}", fallbackConfig.getString("conf.combined")); | ||
// {{end:resolved}} | ||
|
||
// {{start:durations}} | ||
log.info("redis.ttl minutes: {}", fallbackConfig.getDuration("redis.ttl", TimeUnit.MINUTES)); | ||
log.info("redis.ttl seconds: {}", fallbackConfig.getDuration("redis.ttl", TimeUnit.SECONDS)); | ||
// {{end:durations}} | ||
|
||
// {{start:memorySize}} | ||
// Any path in the configuration can be treated as a separate Config object. | ||
Config uploadService = fallbackConfig.getConfig("uploadService"); | ||
log.info("maxChunkSize bytes: {}", uploadService.getMemorySize("maxChunkSize").toBytes()); | ||
log.info("maxFileSize bytes: {}", uploadService.getMemorySize("maxFileSize").toBytes()); | ||
// {{end:memorySize}} | ||
|
||
// {{start:whitelist}} | ||
List<Integer> whiteList = fallbackConfig.getIntList("conf.nested.whitelistIds"); | ||
log.info("whitelist: {}", whiteList); | ||
List<String> whiteListStrings = fallbackConfig.getStringList("conf.nested.whitelistIds"); | ||
log.info("whitelist as Strings: {}", whiteListStrings); | ||
// {{end:whitelist}} | ||
|
||
|
||
// {{start:booleans}} | ||
log.info("yes: {}", fallbackConfig.getBoolean("featureFlags.featureA")); | ||
log.info("true: {}", fallbackConfig.getBoolean("featureFlags.featureB")); | ||
// {{end:booleans}} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
conf { | ||
name = "default" | ||
title = "Simple Title" | ||
nested { | ||
whitelistIds = [1, 22, 34] | ||
} | ||
|
||
combined = ${conf.name} ${conf.title} | ||
} | ||
|
||
featureFlags { | ||
featureA = "yes" | ||
featureB = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
conf { | ||
name = "overrides" | ||
} | ||
|
||
redis { | ||
ttl = 5 minutes | ||
} | ||
|
||
uploadService { | ||
maxChunkSize = 512k | ||
maxFileSize = 5G | ||
} |