Skip to content

Property files

zenbones edited this page Mar 14, 2011 · 1 revision

Property Files

The project uses a layered set of configuration files, in the standard java property file format. Most of these are kept in the foundation module, at the usual location (src/main/resources/property/com/<project>/foundation, see The Project Structure), as is the controlling Spring configuration file, foundation.xml, also at the usual location (src/main/resources/spring/com/<project>/foundation). The practical consideration is that you must load foundation.xm into any Spring context in which you want access to property substitution (which is basically always). The property file locations as defined within foundation.xml are...

<value>classpath:com/project/foundation/global.properties</value>
<value>classpath:com/project/foundation/database-${PROJECT_DATABASE}.properties</value>
<value>classpath:com/project/foundation/${PROJECT_ENVIRONMENT}/environment.properties</value>
<value>classpath:com/project/foundation/${PROJECT_ENVIRONMENT}/database-${PROJECT_DATABASE}.properties</value>
<value>classpath:com/project/${PROJECT_APPLICATION}/application.properties</value>
<value>classpath:com/project/${PROJECT_APPLICATION}/database-${PROJECT_DATABASE}.properties</value>
<value>classpath:com/project/${PROJECT_APPLICATION}/${PROJECT_ENVIRONMENT}/application.properties</value>
<value>classpath:com/project/${PROJECT_APPLICATION}/${PROJECT_ENVIRONMENT}/database-${PROJECT_DATABASE}.properties</value>
<value>classpath:com/project/${PROJECT_APPLICATION}/${PROJECT_PRODUCT}/application.properties</value>
<value>classpath:com/project/${PROJECT_APPLICATION}/${PROJECT_PRODUCT}/database-${PROJECT_DATABASE}.properties</value>
<value>classpath:com/project/${PROJECT_APPLICATION}/${PROJECT_PRODUCT}/${PROJECT_ENVIRONMENT}/application.properties</value>
<value>classpath:com/project/${PROJECT_APPLICATION}/${PROJECT_PRODUCT}/${PROJECT_ENVIRONMENT}/database-${PROJECT_DATABASE}.properties</value>
<value>file:/home/im/config/${PROJECT_APPLICATION}/local.properties</value>
<value>file:${user.home}/.m2/local.properties</value>
<value>file:${user.home}/.m2/database-${PROJECT_DATABASE}.properties</value>

...which represents a layer cake of properties, with each lower definition overriding the one above it.

  1. foundation/global.properties - The global set of properties
  2. foundation/database-<mysql|hsqldb>.properties - Database specific properties, the currently supported databases are 'mysql' (mysql), and 'hsqldb' (hsqldb)
  3. <env>/environment.properties - Environment specific properties, the directories are 'de' (development), 'st' (stage), 'pr' (production), and 'qa' (qa)
  4. <env>/database-<mysql|hsqldb>.properties - Environment specific database overrides
  5. <module>/application.properties - Application specific properties (or, rather, module specific)
  6. <module>/database-<mysql|hsqldb>.properties - Application specific database overrides
  7. <product>/application.properties - Product specific properties
  8. <product>/database-<mysql|hsqldb>.properties - Product specific database overrides
  9. <user home>/.m2/local.properties - Local overrides that are not on the classpath, but found at <user home>/.m2/local.properties (feel free to create the file)
  10. <user home>/.m2/database-<mysql|hsqldb>.properties - Local database overrides

All properties are available to any Spring file via the standard nomenclature of ${property.key}. In each case, properties are evaluated recursively, so it's acceptable to use one property in another property's definition, as in 'my.property=${this.property) plus ${that.property}'.

Feel free to add properties as necessary, but try to be frugal. These files tend to grow, so think about the minimum set of properties the code base as a whole can get away with before adding new ones. When you do add properties, try to use java standard dot-notated keys that make sense, as in 'jdbc.edna.password', or 'smtp.host'.

Environment Variables

There are 4 special environment variables you need to know about...

  1. . PROJECT_ENVIRONMENT - Defines which <env> directory should be used for the environment properties, defaults to 'pr'
  2. . PROJECT_DATABASE - Defines which database should be used for the database properties, defaults to 'mysql'
  3. . PROJECT_APPLICATION - Defines the <module> directory that will be used for application specific properties, defaults to 'foundation'
  4. . PROJECT_PRODUCT - Defines the 'product' into which the module is being integrated, defaults to 'any'

You can set these as necessary *before* generating a Spring application context via calls to System.setProperty(), or within a Spring bean loaded into your context, like this...

<bean class="org.smallmind.nutsnbolts.spring.SystemPropertyBeanFactoryPostProcessor">
   <property name="order" value="0"/>
   <property name="propertyMap">
      <map>
         <entry key="PROJECT_ENVIRONMENT" value=de|st|pr|qa"/>
         <entry key="PROJECT_DATABASE" value="mysql|hsqldb"/>
         <entry key="PROJECT_APPLICATION" value="your module"/>
         <entry key="PROJECT_PRODUCT" value="your product"/>
      </map>
   </property>
</bean>

Clone this wiki locally