public class ConfigurationLoader extends Object
Often web applications need to load configuration from outside. T
Typical usage would be MyConfig config=ConfigurationLoad.from(...).as(MyConfig.class)
where the MyConfig interface defines a bunch of methods named after
the property name:
interface MyConfig { File rootDir(); int retryCount(); String domainName(); ... }
Method calls translate to respective property lookup. For example, config.rootDir()
would
be equivalent to new File(properties.get("rootDir"))
.
The method name can include common prefixes, such as "get", "is", and "has", and those portions
will be excluded from the property name. Thus the rootDir()
could have been named getRootDir()
.
Modifier and Type | Method and Description |
---|---|
<T> T |
as(Class<T> type)
Creates a type-safe proxy that reads from the source specified by one of the fromXyz methods.
|
static ConfigurationLoader |
from(File configPropertyFile)
Loads the configuration from the specified property file.
|
static ConfigurationLoader |
from(Map<String,String> props) |
static ConfigurationLoader |
from(Properties props)
Loads the configuration from the specified
Properties object. |
static ConfigurationLoader |
fromEnvironmentVariables()
Creates
ConfigurationLoader that uses environment variables as the source. |
static ConfigurationLoader |
fromSystemProperties()
Creates
ConfigurationLoader that uses all the system properties as the source. |
public static ConfigurationLoader from(File configPropertyFile) throws IOException
IOException
public static ConfigurationLoader from(Properties props) throws IOException
Properties
object.IOException
public static ConfigurationLoader from(Map<String,String> props) throws IOException
IOException
public static ConfigurationLoader fromSystemProperties() throws IOException
ConfigurationLoader
that uses all the system properties as the source.IOException
public static ConfigurationLoader fromEnvironmentVariables() throws IOException
ConfigurationLoader
that uses environment variables as the source.
Since environment variables are often by convention all caps, while system properties
and other properties tend to be camel cased, this method creates a case-insensitive configuration
(that allows retrievals by both "path" and "PATH" to fill this gap.IOException
public <T> T as(Class<T> type)
Copyright © 2017. All rights reserved.