Loading Bean Property Values ​​from a .xml Context

We are currently loading the original JDBC values ​​from the properties file as follows:

<context:property-placeholder location="classpath:master.properties" ignore-unresolvable="true" />

<bean id="mainDataSource" class="com.jolbox.bonecp.BoneCPDataSource"
    destroy-method="close">
    <property name="driverClass" value="${database.driver}" />
    <property name="jdbcUrl" value="${database.url}" />
    <property name="username" value="${database.user}" />
    <property name="password" value="${database.password}" />
    <property name="idleConnectionTestPeriod" value="60" />
    <property name="idleMaxAge" value="240" />
    <property name="maxConnectionsPerPartition" value="2" />
    <property name="minConnectionsPerPartition" value="2" />
    <property name="partitionCount" value="3" />
    <property name="acquireIncrement" value="10" />
    <property name="statementsCacheSize" value="50" />
    <property name="releaseHelperThreads" value="3" />
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"
    scope="singleton">
    <property name="targetDataSource">
        <ref local="mainDataSource" />
    </property>
</bean>

This segment. works great with classpath based app.properties file and gets rid of app.properties.

We would like to load these values ​​from context.xml (either placed in META-INF, or $CATALINA_HOME/conf/context.xml). This will help us load the correct values ​​on prod / staging servers.

Thank any help or alternative method / suggestions. (If a similar question has already been given, please share the link) Thank you!

+4
source share
3 answers

, Tomcat.xml, Spring JNDI . , , .

, , :

1. $CATALINA_HOME/conf/context.xml

<GlobalNamingResources>

    <Resource type="javax.sql.DataSource" 
              name="dsName"
              factory="com.jolbox.bonecp.BoneCPDataSource" 
              driverClassName="your.driver.classname"
              jdbcUrl="your:driver:url" 
              username="username"
              password="password" 
              idleMaxAge="240" 
              idleConnectionTestPeriod="60"
              partitionCount="3" 
              acquireIncrement="10" 
              maxConnectionsPerPartition="2"
              minConnectionsPerPartition="2" 
              statementsCacheSize="50"
              releaseHelperThreads="3" />

</GlobalNamingResources>

2. META-INF/context.xml

<Context path="/YourApp">
    <ResourceLink description="Datasource for YourApp" 
                  global="jdbc/dsName"
                  name="jdbc/dsName" 
                  type="javax.sql.DataSource" />        
</Context>

3. Spring JNDI

<beans xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/jee classpath:/org/springframework/ejb/config/spring-jee-3.0.xsd">

    <jee:jndi-lookup id="dataSource" 
                     jndi-name="java:comp/env/jdbc/dsName" />

4. datasource

, $CATALINA_HOME/lib, Tomcat, . WEB-INF/lib .

+3

, , , ():

spring:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="ignoreResourceNotFound" value="true"/>
      <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
            <value>classpath:camel.properties</value>
            <value>classpath:email.properties</value>
            <value>${external_config}</value>
        </list>
    </property>
</bean>

.
, , , :

-Dexternal_config=file:c:/staging.properties

, .

, ${external_config} , . Spring external_config, . , spring ignoreResourceNotFound = true;

+3

Spring 3.1 ​​ , . , , . test.properties, live.properties -Dspring.profiles.active = Test. .

http://spring.io/blog/2011/02/11/spring-framework-3-1-m1-released/

Another option outlined above is the transition to a container-managed. The connection pool is viewed through JNDI. Define a JNDI data source named xyz the same on each server, and then:

+1
source

All Articles