Config file in Scala

Source: Internet
Author: User
Tags json root directory

Typesafe Config library, pure Java writing, 0 external dependencies, code simplification, flexible functionality, API-friendly. Supports Java properties, JSON, JSON superset format Hocon, and environment variables. It is also a Akka configuration management library.

Overview

Pure Java implementation, without any reliance on
full test
support: Java properties, JSON, and a human-friendly JSON superset
can merge various formats of the configuration file
can be through the file , URLs, classpath load configurations
support multi-layered nesting configurations to
identify the Java system Properties, such as java-dmyapp.foo.bar=10
can be converted in length, size, and other units. such as timeout=10s in the configuration file, it can be converted into any millisecond or
type conversion, such as Yes can be converted to a Boolean type of True
JSON superset Features:
    comments
    includes
    substitutions ("foo": ${bar}, "foo": Hello ${who})
    properties-like notation (a.b=c)
    Less noisy, more lenient syntax
    substitute environment variables (logdir=${home}/logs)

Example
The Application.conf,application.json and application.properties files under Classpath are loaded by default. Loaded via Configfactory.load ().

# These is our own config values defined by the app
Simple-app {
    answer=42
}
# Here we override some values used by a library
simple-lib.foo= "This value comes from Simple-app ' s application.conf" C1/>simple-lib.whatever = "This value comes from Simple-app ' s application.conf"
public class Simplelibcontext {
    private config config;

    Specify configuration file public
    simplelibcontext (config config) {
        this.config = config;
        Config.checkvalid (Configfactory.defaultreference (), "Simple-lib");
    }

    The default load classpath under Application.* public
    Simplelibcontext () {This
        (Configfactory.load ());
    }

    Print public
    void printsetting (String path) {
        System.out.println ("the setting '" + Path + "is:" + config.getst Ring (path));

    public static void Main (string[] args) {
        Simplelibcontext s = new Simplelibcontext ();
        S.printsetting ("Simple-app.answer");
    }
}

the operation of the config file
You can configure content to be hierarchical, or you can use "." A line is written as a separate number
Such as:

Akka {
 host = "0.0.0.0"
 port = 9999

Write a line with ".", such as:

Akka.host = "0.0.0.0"

When there are multiple profiles, you can use the include introduction, as

Calculator {
 include "common"
 akka {
 remote.netty.port = 2552
 }
}

In addition:
AKKA2 using the Typesafe Config library, you can load the configuration file with Configfactory.load (), load the application.conf under Classpath by default, Application.json and Application.properties file. Actorsystem will merge these configurations with the reference.conf (merge).

If you want to write the Akka app, write the configuration in the application.conf file in the Classpath root directory.
If you are writing a Akka-based Lib package, write the configuration in the reference.conf file in the root directory within the jar package.

If there is a conflict with multiple config files, the solution has
1. A.withfallback (b)? A and B merge, if you have the same key, whichever is a
2. A.withonlypath (String path)? Just take the configuration under path A.
3. A.withoutpath (String Path)//Only take the configuration outside the path of a

You can refer to the API description of CONIFG

    /** * Obtains the default reference configuration, which is currently created * by merging all resources "re Ference.conf "found on the classpath and * Overriding the result with system properties.
     The returned reference * configuration would already have substitutions resolved.
     * * <p> * Libraries and frameworks should ship with a ' reference.conf ' in their * jar. * * <p> * The reference config must be looked on the class loader that contains * the libraries T The "reference.conf" for each library can is found, hat you want
     Use * {@link #defaultReference (ClassLoader)} If the context class loader are not * suitable.
     * * <p> * The {@link #load ()} methods merge this configuration for you * automatically. * * <p> * Future versions-reference configuration in more places. It * is not guaranteed the this mEthod <em>only</em> looks at * "reference.conf". * * @return The default reference config for context class loader */public static config Defaultreference (
    ) {return defaultreference (Checkedcontextclassloader ("defaultreference")); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.