创建不同属性文件的对象池?

时间:2014-01-21 13:48:50

标签: java spring spring-3

如何使用两个属性文件创建两个对象池?目前我使用一个属性文件。

ConnectionsFactory.java

public class ConnectionsFactory implements PoolableObjectFactory {

  private Properties connectionProperties;
  public ConnectionsFactory(Properties connectionProperties) {
      this.connectionProperties= connectionProperties;
  }

 //here makeObject, destroyObject and validateObject methods have been overridden

}

ConnectionPool.java

public class ConnectionPool {
 private GenericObjectPool objectPool; .
 private int maxActive = 50;
 private int maxIdle = -1;
 public SiperianClientPool(SiperianClientFactory factory) {
    this.objectPool = new GenericObjectPool();
    this.objectPool.setFactory(factory);
    this.objectPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    this.objectPool.setTestOnBorrow(true);
    this.objectPool.setTestOnReturn(true);
    this.objectPool.setMaxActive(this.maxActive);
    this.objectPool.setMaxIdle(this.maxIdle);
    }

  //here i provided two methods one to get the connection and the other to return the connection to the pool

}

connectionProperties.properties

uname=xxx
pwd=xxxx
host=xxx
java.naming.provider.url=someurl

的applicationContext.xml

<bean id="connectionPool" class="com.mycompany.ConnectionPool">
        <constructor-arg ref="connectionsFactory"/>
</bean>

<bean id="connectionsFactory" class="com.mycompany.ConnectionsFactory">
        <constructor-arg ref="propertyFactory"/>
</bean>


<bean id="propertyFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="locations">
            <list>  
                <value>classpath:conf/connectionProperties.properties</value>
            </list>
        </property>
</bean>

以上配置工作正常,没有任何问题。 到目前为止,我只有一个属性文件,所有属性都属于一个地理位置。 现在我需要更改属性文件以支持多个地理位置,每个地理位置都有如下属性。

connectionProperties_US.properties

uname=xxx
pwd=xxxx
host=xxx

connectionProperties_UK.properties

uname=xxx
pwd=xxxx
host=xxx

connectionProperties_Germany.properties

uname=xxx
pwd=xxxx
host=xxx

1 个答案:

答案 0 :(得分:0)

您可以使用SPEL实现此目的。例如,您可以使用一个返回语言环境的静态方法,并使用该值来定义属性文件名。

<value>classpath:conf/connectionProperties_#{ T(java.util.Locale).getCountry()}.properties</value>