public class Environment extends Object
Extension
modules might configure application
services differently depending on the environment too. For example: turn on/off caches,
reload files, etc.
The application.env
property controls the active environment names.Constructor and Description |
---|
Environment(ClassLoader classLoader,
com.typesafe.config.Config conf,
List<String> actives)
Creates a new environment.
|
Environment(ClassLoader classLoader,
com.typesafe.config.Config conf,
String... actives)
Creates a new environment.
|
Modifier and Type | Method and Description |
---|---|
static com.typesafe.config.Config |
defaults()
Creates a default configuration properties with some common values like: application.tmpdir,
application.charset and pid (process ID).
|
List<String> |
getActiveNames()
Active environment names.
|
ClassLoader |
getClassLoader()
Application class loader.
|
com.typesafe.config.Config |
getConfig()
Application configuration.
|
Map<String,String> |
getProperties(String key)
List all the properties under the given key.
|
Map<String,String> |
getProperties(String key,
String prefix)
List all the properties under the given key.
|
String |
getProperty(String key)
Get a property under the given key or
null when missing. |
String |
getProperty(String key,
String defaults)
Get a property under the given key or use the given default value when missing.
|
boolean |
isActive(String name,
String... names)
Test is the given environment names are active.
|
Optional<Class> |
loadClass(String className)
Loaded class or empty.
|
static Environment |
loadEnvironment(EnvironmentOptions options)
This method search for an application.conf file in three location
(first-listed are higher priority):
${user.dir}/conf: This is a file system location, useful is you want to externalize
configuration (outside of jar file).
${user.dir}: This is a file system location, useful is you want to externalize
configuration (outside of jar file)
classpath:// (root of classpath).
|
static String |
pid()
Find JVM process ID.
|
static com.typesafe.config.Config |
systemEnv()
Creates a
Config object from System.getenv() . |
static com.typesafe.config.Config |
systemProperties()
Creates a
Config object from System.getProperties() . |
String |
toString() |
public Environment(@Nonnull ClassLoader classLoader, @Nonnull com.typesafe.config.Config conf, @Nonnull String... actives)
classLoader
- Class loader.conf
- Application configuration.actives
- Active environment names.@Nonnull public String getProperty(@Nonnull String key, @Nonnull String defaults)
key
- Property key.defaults
- Default value.@Nullable public String getProperty(@Nonnull String key)
null
when missing.key
- Property key.null
when missing.@Nonnull public Map<String,String> getProperties(@Nonnull String key)
user.name = "name" user.password = "pass"A call to
getProperties("user")
give you a map like:
{user.name: name, user.password: pass}
key
- Key.@Nonnull public Map<String,String> getProperties(@Nonnull String key, @Nullable String prefix)
user.name = "name" user.password = "pass"A call to
getProperties("user", "u")
give you a map like:
{u.name: name, u.password: pass}
key
- Key.prefix
- Prefix to use or null
for none.@Nonnull public com.typesafe.config.Config getConfig()
@Nonnull public List<String> getActiveNames()
public boolean isActive(@Nonnull String name, String... names)
name
- Environment name.names
- Optional environment names.@Nonnull public ClassLoader getClassLoader()
@Nonnull public Optional<Class> loadClass(@Nonnull String className)
className
- Class name.@Nonnull public static com.typesafe.config.Config systemProperties()
Config
object from System.getProperties()
.@Nonnull public static com.typesafe.config.Config systemEnv()
Config
object from System.getenv()
.@Nonnull public static Environment loadEnvironment(@Nonnull EnvironmentOptions options)
options
- Options like basedir, filename, etc.@Nonnull public static com.typesafe.config.Config defaults()
Copyright © 2020. All rights reserved.