public class AppConfig extends Object implements Map<String,String>
name.properties, where name is a name of a deployment environment, such as "development",
"staging", "production", etc.
You can also provide a global file, properties from which will be loaded in all environments: global.properties.
In all cases the files need to be on the classpath in package /app_config.
Environment-specific file will have an "environment" part of the file name match to an environment variable called
ACTIVE_ENV
or system property active_env.
The system property will override the environment variable!
Such configuration is easy to achieve in Unix shell:
export ACTIVE_ENV=test
If environment variable ACTIVE_ENV is missing, it defaults to "development".
You can also provide an environment as a system property active_env. System property overrides environment
variable ACTIVE_ENV
/app_config package:
ACTIVE_ENV=staging, then properties will be loaded from the following files:
java -cp $CLASSPATH com.myproject.Main -Dapp_config.properties=/opt/directory1/myproject.properties
The file-based configuration overrides classpath one. If you have a property defined in both, the classpath configuration will be completely ignored and the file property will be used.
first.name=John
phrase= And the name is ${first.name}
than this code will print And the name is John:
System.out.println(p("phrase"));
Note: The order of properties does not matter.| Constructor and Description |
|---|
AppConfig() |
| Modifier and Type | Method and Description |
|---|---|
static String |
activeEnv()
Returns current environment name as defined by environment variable
ACTIVE_ENV. |
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
Set<Map.Entry<String,String>> |
entrySet() |
String |
get(Object key) |
static Map<String,String> |
getAllProperties() |
static Property |
getAsProperty(String key)
Returns property instance corresponding to key.
|
static List<String> |
getKeys(String prefix)
Returns all keys that start with a prefix
|
static List<String> |
getProperties(String prefix)
Return all numbered properties with a prefix.
|
static String |
getProperty(String key)
Returns property value for a key.
|
static void |
init() |
boolean |
isEmpty() |
static boolean |
isInDevelopment() |
static boolean |
isInProduction() |
static boolean |
isInStaging() |
static boolean |
isInTestEnv() |
static boolean |
isInTestMode()
Checks if running in a context of a test by checking of a presence of a class
org.junit.Test on classpath. |
Set<String> |
keySet() |
static String |
p(String key)
Gets property, synonym for
getProperty(String). |
static Boolean |
pBoolean(String propertyName)
Read property as
Boolean. |
static Double |
pDouble(String propertyName)
Read property as
Double. |
static Float |
pFloat(String propertyName)
Read property as
Float. |
static Integer |
pInteger(String propertyName)
Read property as
Integer. |
String |
put(String key,
String value) |
void |
putAll(Map<? extends String,? extends String> m) |
static void |
reload()
Used in tests.
|
String |
remove(Object key) |
static void |
setActiveEnv(String activeEnv)
You can change the environment dynamically.
|
static String |
setProperty(String name,
String value)
Sets a property in memory.
|
int |
size() |
Collection<String> |
values() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAllpublic static void setActiveEnv(String activeEnv)
activeEnv - new environment valuepublic static void init()
public static void reload()
public static String setProperty(String name, String value)
name - - name of propertyvalue - - value of propertypublic static Property getAsProperty(String key)
key - key for property.public static String getProperty(String key)
key - key of property.null if not found.public static String p(String key)
getProperty(String).key - key of propertypublic boolean containsKey(Object key)
containsKey in interface Map<String,String>public boolean containsValue(Object value)
containsValue in interface Map<String,String>public static String activeEnv()
ACTIVE_ENV.ACTIVE_ENV.public static boolean isInTestMode()
org.junit.Test on classpath.org.junit.Test is on classpath, otherwise returns falsepublic static boolean isInTestEnv()
ACTIVE_ENV is "testenv".public static boolean isInProduction()
ACTIVE_ENV is "production".public static boolean isInDevelopment()
ACTIVE_ENV is "development".public static boolean isInStaging()
ACTIVE_ENV is "staging".public static List<String> getKeys(String prefix)
prefix - prefix for properties.public static List<String> getProperties(String prefix)
prop.1=one
prop.2=two
.. and this method is called:
List props = AppConfig.getProperties("prop");
then the resulting list will have all properties starting from prop.
This method presumes consecutive numbers in the suffix.prefix - prefix of numbered properties.public static Integer pInteger(String propertyName)
Integer.propertyName - name of property.Integer.public static Double pDouble(String propertyName)
Double.propertyName - name of property.Double.public static Float pFloat(String propertyName)
Float.propertyName - name of property.Float.Copyright © 2022 JavaLite. All rights reserved.