注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

和申的个人主页

专注于java开发,1985wanggang

 
 
 

日志

 
 

Loading Application properties from a database  

2008-10-08 10:57:08|  分类: Java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Contents

Overview

This example will show you how to use a DatabaseConfiguration from the Jakarta Commons Configurations project the load 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 avaliable 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 avaliable

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*.

TEST_SCHEMA.APPLICATION_PROPERTIES_TABLE

KEY VALUE
key.one value one
file.location somewhere/on/the/filesystem
pet.dogs.name bart

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

How it works

  1. The DatabaseConfiguration is initialised 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 initialised with the DatabaseConfiguration? bean as its configuration (It can have many but that is not used in this example)
  3. The PropertyPlaceholderConfigurer is initialised 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 avaliable to any bean within the current spring configuration file via the ${} notation.
  5. The PropertiesPrinter is then intialised with the properties file.location, pet.dogs.name and file.location
  6. displayAllProperties() (initethod) 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 avaliable within the application context.

CommonsConfigurationFactoryBean [Sprint Modules] Creates a properties objects using classes from commons configuration

DatabaseConfiguration [Commons Configuration] loads properties from a database table.

Spring Configuration

<!-- Required to donnect to datasource -->
<bean name="PropertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="CommonsConfigurationFactoryBean"/>
</bean>

<bean name="CommonsConfigurationFactoryBean"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<constructor-arg ref="DatabaseConfiguration"/>
</bean>

<bean name="DatabaseConfiguration"
class="org.apache.commons.configuration.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"/>
</bean>

<!-- Included to elaborate functionality -->

<bean name="PropertiesPrinter " class="example.PropertiesPrinter"
initMethod="displayAllProperties">
<property name="fileLocation" value="${file.location}"/>
<property name="petDogsName" value="${pet.dogs.name}"/>
<property name="keyOne" value="${key.one}"/>
</bean>

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 Initialisation Output

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

Related Links

  • Spring
  • License

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

    About the Author

    daniel_at_work



    Occupation: Software Developer (Senior)
    Location: Australia Australia
      评论这张
     
    阅读(738)| 评论(0)
    推荐 转载

    历史上的今天

    在LOFTER的更多文章

    评论

    <#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
     
     
     
     
     
     
     
     
     
     
     
     
     
     

    页脚

    网易公司版权所有 ©1997-2016