Click here to Skip to main content
15,666,286 members
Articles / Programming Languages / Java / Java SE
Posted 27 Aug 2008

Tagged as


13 bookmarked

Loading Application Properties from a Database

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
16 Sep 2008CPOL2 min read
Use Spring and Commons Configuration to load application properties from a database


  • Overview
  • Getting Started
    • Required Jars
    • Database Table Used in this Example
  • How It Works
  • Spring Configuration
  • Code Example
  • Application Context Initialization Output
  • Related Links


This example will show you how to use a DatabaseConfiguration from the Jakarta Commons Configurations project and load the Database Properties into your application context.

If you want to take advantage of the features offered in commons configuration to load your application properties into your spring context, you can use spring modules to make the properties loaded from commons configurations available within your application context.

Getting Started

To follow this example for using commons configuration with spring, you will need the jars for spring, spring-modules and commons configuration. You will also need a database available.

Required Jars

To get started with this example, you will need three jar files.

  • spring.jar (Spring Core) [PropertiesPlaceholderConfigurer]
  • spring-modules.jar (Spring Modules) [CommonsConfigurationFactoryBean]
  • commons-configuration.jar (Commons Configuration) [DatabaseConfiguration]

Database Table Used in this Example

For this example, the database has a schema in it called TEST_SCHEMA and a table called APPLICATION_PROPERTIES_TABLE with two columns KEY and VALUE*.


KEY VALUE value one
file.location somewhere/on/the/filesystem bart

* Note that this is only one example of a usable table structure.

How It Works

  1. The DatabaseConfiguration is initialized from the injected datasource and is configured to load the properties from the table TEST_SCHEMA.APPLICATION_PROPERTIES using the column KEY as the key and VALUE as the value.
  2. The CommonsConfigurationFactoryBean is initialized with the DatabaseConfiguration? bean as its configuration (It can have many but that is not used in this example).
  3. The PropertyPlaceholderConfigurer is initialized with properties attribute being set to the CommonsConfigurationFactoryBean. The CommonsConfigurationFactoryBean? is a FactoryBean that creates a Properties object.
  4. The PropertyPlaceholderConfigurer then makes the properties available to any bean within the current spring configuration file via the ${} notation.
  5. The PropertiesPrinter is then initialized with the properties file.location, and file.location.
  6. displayAllProperties() (initMethod) is then called on the PropertiesPrinter? and the following would be output:
File Location : somewhere/on/the/filesystem
Pet dogs name : bart
Key one       : value one

In Summary

  • PropertiesPlaceholderConfigurer [Spring-Core] makes its properties available within the application context.
  • CommonsConfigurationFactoryBean [Sprint Modules] creates a properties object using classes from commons configuration.
  • DatabaseConfiguration [Commons Configuration] loads properties from a database table.

Spring Configuration

<!-- Required to connect to datasource -->
<bean name="PropertyPlaceholderConfigurer"
     <property name="properties" ref="CommonsConfigurationFactoryBean"/>
<bean name="CommonsConfigurationFactoryBean"
       <constructor-arg ref="DatabaseConfiguration"/>
<bean name="DatabaseConfiguration"
        <constructor-arg type="javax.sql.DataSource" ref="someDataSource"/>
        <constructor-arg index="1" value="TEST_SCHEMA.APPLICATION_PROPERTIES_TABLE"/>
        <constructor-arg index="2" value="KEY"/>
        <constructor-arg index="3" value="VALUE"/>

<!-- Included to elaborate functionality -->

<bean name="PropertiesPrinter " class="example.PropertiesPrinter"
    <property name="fileLocation" value="${file.location}"/>
    <property name="petDogsName" value="${}"/>
    <property name="keyOne" value="${}"/>

Code Example

package example;

public class PropertiesPrinter {
   public String fileLocation;
   public String petDogsName;
   public String keyOne;
   public void setFileLocation(String fileLocation) {
      this.fileLocation = fileLocation;

   public void setPetDogsName(String petDogsName) {
      this.petDogsName = petDogsName;
   public void setKeyOne(String keyOne) {
      this.keyOne = keyOne;

   public void displayAllProperties() {
      System.out.println("File Location : " + this.fileLocation);
      System.out.println("Pet dogs name : " + this.petDogsName);
      System.out.println("Key one       : " + this.keyOne);

Application Context Initialization Output

File Location : somewhere/on/the/filesystem
Pet dogs name : bart
Key one       : value one

Related Links


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior)
Australia Australia
First Started developing professionaly in 2004. I have mostly worked across the IBM products with a dash of open source components added in. My IBM Product Experience includes Websphere, DB2, IBM Content Manager EE, RAD. On the Open Source side some of the noteworthy components are Spring, Tapestry, Junit plus a bunch of utility apis.

Comments and Discussions

GeneralExcellent article buddy. Pin
Nelson Garcia4-Oct-09 7:21
Nelson Garcia4-Oct-09 7:21 
madhump9-Oct-08 21:04
madhump9-Oct-08 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.