[JAVA] A little summary about typesafe config

typesafe config

https://github.com/lightbend/config

config loading library created by typesafe (now lightbend)

Preface

――I used it somehow, so I wrote it to organize it. --I use it in Scala

What?

--Setting library written in java without dependent libraries --You can read the configuration file written in JSON (.json) or HOCON (.conf) into the application.

To use

Let's write in build.sbt

libraryDependencies += "com.typesafe" % "config" % "1.3.1"

Load the config file with ConfigFactory.load.

You can get the value from the key set from the returned config object and use it.

The Config object has a method of get ~, and you can read what you wrote in the config file by specifying the type. When reading, an error will occur if the method and the actual value type are different

The code becomes ↓

import com.typesafe.config.ConfigFactory

object ConfigSample {
  def main(args: Array[String]): Unit = {
    val config: Config = ConfigFactory.load()
    
    config.get~("path") //Set value~Use as a mold to read
    // String, Int, Enum, Boolean, etc...
    
    val subconf: Config = config.getConfig("subconfig")
   
 	//When there is a conf file like the one below
 	//A Config object rooted in subconfig is returned in subconf
 	//So`subconf.getString("host")`Then"sub_host"Returns
   /*
   * # application.conf
   * host = "main_host"
   * subconfig {
   *		host = "sub_host"
   * }
   */
   
  }
}

What is loaded?

File path config contents

ConfigFactory.load("file name") // file nameには拡張子を含めない

ʻapplication.conf, ʻapplication.json, ʻapplication.properties, reference.conf` and system properties under src / main / resources

application.conf > application.json > application.properties > reference.conf

The set value is prioritized with

Since reference.conf is read later, it seems good to write the default value when creating a library.

include --If it is a conf file, the settings of the file specified by ʻinclude" file name " can also be imported into the file in which ʻinclude is written. If the file that has been included and thefile that has beenhave the same key setting value, the setting value on thesidehas priority. --The file to include does not have to be a conf file (json, properties are OK)

a.conf



a = "a"


application.conf


include "a"

b = "b"

val config = ConfigFactory.load()
config.getString("a") // a
config.getString("b") // b

Replacement

Property name = $ {replacement source property name}

application_host = "untarakantara.com"

application_url = "https://${application_host}"
ConfigFactory.load().getString("application_url") // `https://untarakantara.com`

Replace from environment variable

Property name = $ {? Environment variable} If there is no environment variable and a value with the same property name as the environment variable name is set in conf, it will be used.

password = ${?PASS_WORD}

Recommended Posts

A little summary about typesafe config
[Personal memo] I learned a little about modifiers
Q & A about JDK
About Eclipse MicroProfile Config
Personal summary about Java
A note about Java GC
A note about the scope
[Swift] Summary about Bool type
A private note about AtomicReference
A little complicated conditional branching
About adding a like function
Here's a summary of what I've been curious about Enums lately