Spring loading application.properties based on tomcat servlet context definition

I need to have development and production settings for our spring project. I understand that you can use profiles for spring, but that’s not what we can do.

What I want to do is place the test-application.properties file in the development environment and create the prod-application.properties file. In the tomcat context definition, we sent the following:

<Context>
    <context-param>
        <param-name>properties_location</param-name>
        <param-value>file:C:\Users\Bill\test-application.properties</param-value>
    </context-param>
</Context>

And we can change the value for production servers. In the spring configuration, we have something like this:

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>${properties_location}</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="false" />
</bean>

But we continue to receive errors, for example:

org.springframework.beans.factory.BeanInitializationException: it is possible not to load properties; nested exception java.io.FileNotFoundException: Failed to open ServletContext resource [/ $ {Properties_location}]

, ?

+5
6

PropertyPlaceholder , . , your-production-config.properties : C:/Users/${user.name}/test-application.properties

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:your-production-config.properties</value>
            <value>file:C:/Users/${user.name}/test-application.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>        
</bean>

prod- classpath - ( , , classpath) - env : C:/Users/${user.name}/test- application.properties

+8
<context:property-placeholder location="file:${catalina.home}/conf/myFirst.properties" ignore-unresolvable="true" />
<context:property-placeholder   location="classpath:second.properties"  ignore-unresolvable="true"  />

, . catalina.home homecode tomcat.

+2

, .

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:application.properties</value>
            <value>file:C:\Users\Bill\prod-application.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="ignoreResourceNotFound" value="true"/>
</bean>

. prod, . prod- prod-application.properties , . , !

+1

. , JNDI .

tomcat/conf/server.xml

<Resource name="jdbc/prod" auth="Container"
          type="javax.sql.DataSource" driverClassName="${database.driverClassName}"
          url="${database.url}"
          username="${database.username}" password="${database.password}"
          maxActive="20" maxIdle="10"
          maxWait="-1"/>

tomcat catalina.properties( Oracle XE, ):

database.driverClassName=oracle.jdbc.driver.OracleDriver
database.url=jdbc:oracle:thin:@//localhost:1521/XE
database.username=user
database.password=password

jdbc.properties ( Oracle XE, )

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:user/password@//localhost:1521/XE

then In Spring applicationContext.xml

<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/prod" />
    <property name="defaultObject" ref="dataSourceFallback" />
</bean>
 
<bean id="dataSourceFallback" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="poolPreparedStatements">
        <value>true</value>
    </property>
    <property name="maxActive">
        <value>4</value>
    </property>
    <property name="maxIdle">
        <value>1</value>
    </property>
</bean>
+1
source

use:

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>C:/Users/Bill/test-application.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="false" />
</bean>

Clear code below web.xml

<Context>
    <context-param>
        <param-name>properties_location</param-name>
        <param-value>file:C:\Users\Bill\test-application.properties</param-value>
    </context-param>
</Context>
0
source

if you use Tcserver and server.xml to configure resources such as database, queues, etc., you can use com.springsource.tcserver.properties.SystemProperties

Delcare this listener in server.xml as below

 <Listener className="com.springsource.tcserver.properties.SystemProperties"
            file.1="${catalina.base}/conf/password.properties"
            file.2="${catalina.base}/conf/server.properties"
            immutable="false"
            trigger="now"/>

Now you can export the properties to the two password.properties and server.properties files.

0
source

All Articles