External properties loader with Tomcat server

2.9k Views Asked by At

I am trying to deploy a spring mvc webapp into a tomcat server. I have been testing locally using the maven-jetty-plugin. In my spring configuration I am using a properties placeholder, and pulling my properties from an external file:

<context:property-placeholder ignore-resource-not-found="true" ignore-unresolvable="true"/>
<bean id="modelPropertyPlaceholder" class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="searchContextAttributes" value="true"/>
    <property name="contextOverride" value="true"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:default.model.properties</value>
            <value>file:/etc/app/app.properties</value>
            <value>${config}</value>
        </list>
    </property>
</bean>

This worked with my jetty plugin...however when I deploy the WAR file to the tomcat server I receive the following error:

 org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'

In my external properties file I have the driver class and connect url defined. as so:

jndi.jpa.rms.datasource=jdbc/testDS
rms.db.driver=com.mysql.jdbc.Driver
rms.db.url=jdbc:mysql://testdatabaseurl:3306/test
rms.db.user=sa
rms.db.password=asfdas
rms.db.checkconnsql=select 1
rms.hibernate.generateddl=false
rms.hibernate.showsql=true
rms.hibernate.dbdialect=org.hibernate.dialect.MySQLDialect

Update:

It seems that tomcat does pickup the external properties file:

 14:23:09.803 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'modelPropertyPlaceholder'
 14:23:09.810 [main] INFO  o.s.w.c.s.ServletContextPropertyPlaceholderConfigurer - Loading properties file from class path resource [default.model.properties]
 14:23:09.810 [main] INFO  o.s.w.c.s.ServletContextPropertyPlaceholderConfigurer - Loading properties file from URL [file:/etc/app/app.properties]
 14:23:09.810 [main] INFO  o.s.w.c.s.ServletContextPropertyPlaceholderConfigurer - Loading properties file from ServletContext resource [/${config}]
 14:23:09.811 [main] WARN  o.s.w.c.s.ServletContextPropertyPlaceholderConfigurer - Could not load properties from ServletContext resource [/${config}]: Could not open ServletContext resource [/${config}]

I'm not sure why tomcat isn't picking up the connection url and driver. Is this a tomcat issue or am I missing something? Thanks

2

There are 2 best solutions below

2
On

It's not a Tomcat issue, you must have made some minor mistake and properties are not loaded.

  1. Are properties files located in proper place on your Tomcat environment?
  2. Are you sure that in those properties files you have not left some keys blank?

If both answers to that questions are true, try changing your config with following:

<context:property-placeholder location="ADD_PATH_TO_YOUR_FILES SEPARATED_WITH_SPACES"/>

I guess that you are mixing here two property placeholders, one 'regular' and the other - bounded to servlet context. I can bet that somehow they get overlapped and one of them is getting silently ignored.

I would just stick to property-placeholder.

0
On

Old question, but...

Judging from the log output, it appears that Spring has not resolved your ${config} reference, or else this would have been substituted into the path given in the last log message:

Could not load properties from ServletContext resource [/${config}]: Could not open ServletContext resource [/${config}]

Here's a case I just encountered where it resolved ${catalina.home}/conf/myprops.properties correctly but was unable to load the referenced file:

Could not load properties from ServletContext resource [/D:/somepath/apache-tomcat/conf/myprops.properties]: Could not open ServletContext resource [/D:/somepath/apache-tomcat/conf/myprops.properties]

I tracked down the error in my case - I needed to prefix the external file reference with 'file:'. That is, it should have been file:${catalina.home}/conf/myprops.properties

(It is possible the Spring version difference may have impacted on the log output for you though also, and that it did resolve the reference but just did not show this in the output. For reference, I'm using Spring 4.1.6 at present).