Spring PropertyPlaceholderConfigurer从DB加载 [英] Spring PropertyPlaceholderConfigurer load from DB

查看:83
本文介绍了Spring PropertyPlaceholderConfigurer从DB加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Custom Spring PropertyPlaceholderConfigurer从DB加载属性?
并且提供给自定义PropertyPlaceholderConfigurer的数据源是否也可能在类路径中使用特定的属性文件?

Is it possible to load properties from DB with Custom Spring PropertyPlaceholderConfigurer? and is it also possible that datasource provided to custom PropertyPlaceholderConfigurer use specific property file in classpath?

我从以下链接中找不到满意的答案?

I could not find satisfied answer from following links?

http://www.mkyong .com / spring / spring-propertyplaceholderconfigurer-example /
http://www.codeproject.com/Articles/28893/Loading-Application-Properties-from-a-Database
PropertyPlaceholderConfigurer查找数据库值并使用属性文件作为后备

推荐答案

Spring Context XML

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="ignoreResourceNotFound" value="false"/>
        <property name="order" value="1" />
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>
    </bean>

 <bean id="dataSourceimos" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass"><value>${imosdb.driver}</value></property>
        <property name="jdbcUrl"><value>${imosdb.url}</value></property>
        <property name="user"><value>${imosdb.username}</value></property>
        <property name="password"><value>${imosdb.password}</value></property>
        <property name="initialPoolSize"><value>${imosdb.initial_pool_size}</value></property>
        <property name="maxPoolSize"><value>${imosdb.max_pool_size}</value></property>
        <property name="minPoolSize"><value>${imosdb.min_pool_size}</value></property>
        <property name="acquireIncrement" value="1"/>
        <property name="acquireRetryAttempts" value="1"/>
        <property name="idleConnectionTestPeriod" value="30"/>
        <property name="preferredTestQuery" value="select 1 from dual"/>
        <property name="checkoutTimeout" value="5000"/>
        <property name="maxAdministrativeTaskTime" value="120"/>
        <property name="numHelperThreads" value="10"/>
    </bean>

    <bean
        class="com.ahmetk.property.DbPropertySourcesPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="order" value="2" />
        <property name="placeholderPrefix" value="${" />
        <property name="placeholderSuffix" value="}" />
        <property name="dataSourceName" value="dataSourceimos" />

        <property name="locations">
            <list>
                <value>classpath:static.properties</value>
                <value>file:static.properties</value>
            </list>
        </property>

    </bean>

    <context:component-scan base-package="com.mkyong.rest" />

    <bean id="transactionBo" class="com.mkyong.transaction.impl.TransactionBoImpl" />
    <bean id="cacheServiceInterface" class="com.ttech.tims.imos.data.cache.CacheServiceImpl" />
    <bean id="iCacheService" class="com.ttech.tims.imos.data.cache.impl.CacheService" />

</beans>

Java PlaceholderClass

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;


public class DbPropertySourcesPlaceholderConfigurer extends PropertyPlaceholderConfigurer 
{
   private static final String DEFAULT_DATASOURCENAME = "dataSource";
   private static final String DEFAULT_DBTABLENAME = "property";
   private static final String DEFAULT_DBKEYCOLUMNNAME = "key";
   private static final String DEFAULT_DBVALUECOLUMNNAME = "value";
   String dataSourceName;
   String dbTableName;
   String dbKeyColumnName;
   String dbValueColumnName;
   @Override
   public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
   {

      DataSource dataSource = (DataSource) beanFactory.getBean(getDataSourceName());
     // DbProperties dbProps = new DbProperties(dataSource);
      final Properties dbProps = new Properties();
      dbProps.put("app.version", "v3");
      setProperties(dbProps);
      super.postProcessBeanFactory(beanFactory);
   }

   public String getDataSourceName() {
      return dataSourceName==null?DEFAULT_DATASOURCENAME:dataSourceName;
   }

   public void setDataSourceName(String dataSourceName) {
      this.dataSourceName = dataSourceName;
   }


}

特别感谢以下页面的作者。

http:// springtips.blogspot.com.tr/

http://ykchee.blogspot.com.tr/2012/09/spring-31-loading-properties-for-xml.html

http:// blog .javaforge.net / post / 31720600427 / configuration-spring-based-web-application-from-database

http://www.javacodegeeks.com/2012/11/spring-3 -1-loading-properties-for-xml-configuration-from-database.html

这篇关于Spring PropertyPlaceholderConfigurer从DB加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆