65.9K
CodeProject 正在变化。 阅读更多。
Home

从数据库加载应用程序属性

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (1投票)

2008年8月28日

CPOL

2分钟阅读

viewsIcon

94344

使用 Spring 和 Commons Configuration 从数据库加载应用程序属性。

目录

  • 概述
  • 入门
    • 必需的Jar包
    • 此示例中使用的数据库表
  • 工作原理
  • Spring配置
  • 代码示例
  • 应用程序上下文初始化输出
  • 相关链接

概述

此示例将向您展示如何使用 Jakarta Commons Configurations 项目中的 DatabaseConfiguration,并将数据库属性加载到您的应用程序上下文中。

如果您想利用 Commons Configuration 中提供的功能将您的应用程序属性加载到您的 Spring 上下文中,您可以使用 Spring Modules 使从 Commons Configuration 加载的属性在您的应用程序上下文中可用。

入门

要遵循此示例,在 Spring 中使用 Commons Configuration,您将需要 Spring、Spring Modules 和 Commons Configuration 的 Jar 包。您还需要一个可用的数据库。

必需的Jar包

要开始此示例,您将需要三个 Jar 文件。

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

此示例中使用的数据库表

在此示例中,数据库中有一个名为TEST_SCHEMA的模式和一个名为APPLICATION_PROPERTIES_TABLE的表,其中包含两列KEYVALUE*

TEST_SCHEMA.APPLICATION_PROPERTIES_TABLE

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

* 请注意,这只是一个可用的表结构的示例。

工作原理

  1. DatabaseConfiguration从注入的数据源初始化,并配置为从表TEST_SCHEMA.APPLICATION_PROPERTIES加载属性,使用列KEY作为键,VALUE作为值。
  2. CommonsConfigurationFactoryBean使用DatabaseConfiguration? bean 作为其配置初始化(它可以有很多,但在此示例中未使用)。
  3. PropertyPlaceholderConfigurer使用设置为CommonsConfigurationFactoryBean的属性属性初始化。CommonsConfigurationFactoryBean? 是一个 FactoryBean,它创建一个Properties对象。
  4. 然后,PropertyPlaceholderConfigurer通过${}符号使属性可用于当前 Spring 配置文件中的任何 bean。
  5. 然后使用属性file.locationpet.dogs.namefile.location初始化PropertiesPrinter
  6. 然后对PropertiesPrinter? 调用displayAllProperties() (initMethod),并将输出以下内容
File Location : somewhere/on/the/filesystem
Pet dogs name : bart
Key one       : value one

总结

  • PropertiesPlaceholderConfigurer [Spring-Core] 使其属性在应用程序上下文中可用。
  • CommonsConfigurationFactoryBean [Sprint Modules] 使用 Commons Configuration 中的类创建一个属性对象。
  • DatabaseConfiguration [Commons Configuration] 从数据库表中加载属性。

Spring配置

<!-- Required to connect 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>

代码示例

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);
   }
}

应用程序上下文初始化输出

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

相关链接

© . All rights reserved.