Class AppConfig

All Implemented Interfaces:

public class AppConfig extends Object implements Map<String,​String>
This class allows configuration of applications for different deployment environments, such as development, test, staging, production, etc. Configuration is done either with property files on the classpath, or on a file system.

1. Classpath configuration

Applications could have environment-specific files, whose names follow this pattern:, 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:

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


If there are four files packed into a /app_config package:
And the ACTIVE_ENV=staging, then properties will be loaded from the following files:

2. File configuration

In addition to properties on classpath, you can also specify a single file for properties to loaded from a file system. Use a system property with a full path to a file like:
     java -cp $CLASSPATH com.myproject.Main
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.

Property substitution

AppConfig allows a property substitution to make it possible to refactor large property files by specifying a repeating value once. If your property file has these properties:
   phrase= And the name is ${}
than this code will print And the name is John:
Note: The order of properties does not matter.
Igor Polevoy
  • Constructor Details

    • AppConfig

      public AppConfig()
  • Method Details

    • setActiveEnv

      public static void setActiveEnv(String activeEnv)
      You can change the environment dynamically. Attention!!! This method should only be used for tests! Careful out there...
      activeEnv - new environment value
    • init

      public static void init()
    • reload

      public static void reload()
      Used in tests.
    • setProperty

      public static String setProperty(String name, String value)
      Sets a property in memory. If property exists, it will be overwritten, if not, a new one will be created.
      name - - name of property
      value - - value of property
      old value
    • getAsProperty

      public static Property getAsProperty(String key)
      Returns property instance corresponding to key.
      key - key for property.
      Property for this key.
    • getProperty

      public static String getProperty(String key)
      Returns property value for a key.
      key - key of property.
      value for this key, null if not found.
    • p

      public static String p(String key)
      Gets property, synonym for getProperty(String).
      key - key of property
      property value
    • getAllProperties

      public static Map<String,​String> getAllProperties()
    • size

      public int size()
      Specified by:
      size in interface Map<String,​String>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<String,​String>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<String,​String>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<String,​String>
    • get

      public String get(Object key)
      Specified by:
      get in interface Map<String,​String>
    • put

      public String put(String key, String value)
      Specified by:
      put in interface Map<String,​String>
    • remove

      public String remove(Object key)
      Specified by:
      remove in interface Map<String,​String>
    • putAll

      public void putAll(Map<? extends String,​? extends String> m)
      Specified by:
      putAll in interface Map<String,​String>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<String,​String>
    • keySet

      public Set<String> keySet()
      Specified by:
      keySet in interface Map<String,​String>
    • values

      public Collection<String> values()
      Specified by:
      values in interface Map<String,​String>
    • entrySet

      public Set<Map.Entry<String,​String>> entrySet()
      Specified by:
      entrySet in interface Map<String,​String>
    • activeEnv

      public static String activeEnv()
      Returns current environment name as defined by environment variable ACTIVE_ENV.
      current environment name as defined by environment variable ACTIVE_ENV.
    • isInTestMode

      public 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.
      true if class org.junit.Test is on classpath, otherwise returns false
    • isInTestEnv

      public static boolean isInTestEnv()
      true if environment name as defined by environment variable ACTIVE_ENV is "testenv".
    • isInProduction

      public static boolean isInProduction()
      true if environment name as defined by environment variable ACTIVE_ENV is "production".
    • isInDevelopment

      public static boolean isInDevelopment()
      true if environment name as defined by environment variable ACTIVE_ENV is "development".
    • isInStaging

      public static boolean isInStaging()
      true if environment name as defined by environment variable ACTIVE_ENV is "staging".
    • getKeys

      public static List<String> getKeys(String prefix)
      Returns all keys that start with a prefix
      prefix - prefix for properties.
    • getProperties

      public static List<String> getProperties(String prefix)
      Return all numbered properties with a prefix. For instance if there is a file:
      .. 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.
      list of property values.
    • pInteger

      public static Integer pInteger(String propertyName)
      Read property as Integer.
      propertyName - name of property.
      property as Integer.
    • pDouble

      public static Double pDouble(String propertyName)
      Read property as Double.
      propertyName - name of property.
      property as Double.
    • pLong

      public static Long pLong(String propertyName)
      Read property as Long.
      propertyName - name of property.
      property as Long.
    • pFloat

      public static Float pFloat(String propertyName)
      Read property as Float.
      propertyName - name of property.
      property as Float.
    • pBoolean

      public static Boolean pBoolean(String propertyName)
      Read property as Boolean.
      propertyName - name of property.
      property as Boolean.