Question

I have a package for an executable jar.I want to externalise the db properties so I use a reference to a properties file from my appContext.xml.

I have enabled TRACE level logging and I see that I don't get any errors against this line

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

However,further down when I try to access the properties configured in the file in my dataSource bean like

<bean id="dataSource"
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${db.driverClassName}"/>
    <property name="url" value="${db.url}" />
    <property name="username" value="${db.username}" />
    <property name="password" value="${db.password}" />
</bean>

I get the following error.

Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${db.driverClassName}]

I have done the following to confirm that the file is being located correctly on the classpath

  1. I deliberately changed the property file name and I could see that I induced a FileNotFound Exception.
  2. I have verified the classpath contains a reference to this external property file.

I'm not sure what I could be missing.Is there anything wrong in the way I refer to the property file or how I refer to the individual properties in my datasource bean ?

I might as well add that I'm using Spring annotation for most beans and use the appcontext for declaration of datasource/transaction manager etc only.

Please help,as I have now spent considerable time on this seemingly trivial piece and seemed to have hit a dead end.

the spring config file is here

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- Auto scan the components -->
    <context:component-scan base-package="patternengine" />
    <context:annotation-config />
    <tx:annotation-driven proxy-target-class="false"/>
    <aop:aspectj-autoproxy  proxy-target-class="false"/>

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

    <bean id="dataSource"
             class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driverClassName}"/>
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

    <bean id="serviceArgs" class="com.splunk.ServiceArgs">
        <property name="username" value="dashboardad" />
        <property name="password" value="splunkuat" />
        <property name="host" value="silrse.com" />
        <property name="port" value="8089" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>

    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
         <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>

    <property name="annotatedClasses">
    <list>
        <value>patternengine.entity.SplunkResult</value>
        <value>patternengine.entity.MinuteGroupedSplunkResult</value>
        <value>patternengine.entity.ApplicationComponent</value>
        <value>patternengine.entity.ApplicationHostSourceDetail</value>
        <value>patternengine.entity.ConfiguredApplication</value>
        <value>patternengine.entity.ConfiguredException</value>
        <value>patternengine.entity.IWatchTransaction</value>
        <value>patternengine.entity.MinuteGroupedIWatchTransaction</value>
        <value>patternengine.entity.AppMap</value>
    </list>
    </property>

    </bean>

    <bean id="transactionManager"
     class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

 </beans>

the property file is here

db.driverClassName=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@hdvash:1521:ECDDDDA01
db.username=rmt_perf_db
db.password=qwerty123

the directory structure is as follows directory structure of the project is here

the startup script is as follows

JAVA_HOME=/home/a_cfp_asp/tomcat/tc7/7.0.39/java
echo 'JAVA_HOME IS ' $JAVA_HOME
PATH=$PATH:$java_home/bin
#BASE_DIR='../'
BASE_DIR='/home/a_cfp_asp/pattern_engine_2b/jobs/dataprocessor/'
CONFIG_ROOT=$BASE_DIR'config'
LIB_ROOT=$BASE_DIR'lib'
jar_path=$LIB_ROOT/antlr-2.7.7.jar:$LIB_ROOT/aopalliance-1.0.jar:$LIB_ROOT/asm-3.1.jar:$LIB_ROOT/aspectjrt-1.6.11.jar:$LIB_ROOT/aspectjweaver-1.6.11.jar:$LIB_ROOT/avalon-framework-4.1.3.jar:$LIB_ROOT/cglib-2.2.jar:$LIB_ROOT/commons-lang3-3.0.jar:$LIB_ROOT/commons-logging-1.1.jar:$LIB_ROOT/dom4j-1.6.1.jar:$LIB_ROOT/hibernate-commons-annotations-4.0.1.Final.jar:$LIB_ROOT/hibernate-core-4.2.3.Final.jar:$LIB_ROOT/hibernate-entitymanager-3.6.8.Final.jar:$LIB_ROOT/hibernate-envers-4.2.3.Final.jar:$LIB_ROOT/hibernate-jpa-2.0-api-1.0.1.Final.jar:$LIB_ROOT/javassist-3.12.0.GA.jar:$LIB_ROOT/javassist-3.15.0-GA.jar:$LIB_ROOT/jboss-logging-3.1.0.GA.jar:$LIB_ROOT/jboss-transaction-api_1.1_spec-1.0.1.Final.jar:$LIB_ROOT/log4j-1.2.14.jar:$LIB_ROOT/logkit-1.0.1.jar:$LIB_ROOT/oracle-11.1.0.6.jar:$LIB_ROOT/pe-common-1.0.jar:$LIB_ROOT/pe-dataprocessor.jar:$LIB_ROOT/slf4j-api-1.6.1.jar:$LIB_ROOT/splunk-1.1.0.jar:$LIB_ROOT/spring-aop-3.1.1.RELEASE.jar:$LIB_ROOT/spring-asm-3.1.1.RELEASE.jar:$LIB_ROOT/spring-beans-3.1.1.RELEASE.jar:$LIB_ROOT/spring-context-3.1.1.RELEASE.jar:$LIB_ROOT/spring-context-support-3.1.1.RELEASE.jar:$LIB_ROOT/spring-core-3.1.1.RELEASE.jar:$LIB_ROOT/spring-expression-3.1.1.RELEASE.jar:$LIB_ROOT/spring-jdbc-3.1.1.RELEASE.jar:$LIB_ROOT/spring-orm-3.1.1.RELEASE.jar:$LIB_ROOT/spring-tx-3.1.1.RELEASE.jar
config_path=$CONFIG_ROOT/db.properties
MYCLASSPATH=.:$config_path:$jar_path

java -cp$MYCLASSPATH com.patternengine.service.impl.DataProcessorService "Dashboard Application" 30
Was it helpful?

Solution

I'm almost certain that your properties file is not on the classpath as you're intending.

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

Note that if you remove the ignore-unresolvable, Spring will fail with

Invalid bean definition with name 'dataSource' defined in class path resource [<yourfile>.xml]: Could not resolve placeholder 'db.driverClassName' in string value "${db.driverClassName}"

If it could not resolve, two things might have happened: your file didn't contain the property or your file wasn't found. The former is much more likely with the details you've given us.

EDIT: Based on your configuration, the db.properties file is in the folder config relative to the classpath root. You need to access it with config/db.properties. Also, this might be relevant: Spring classpath prefix difference

OTHER TIPS

What does your project structure look like? I know you said you verified that the file was included in the classpath but let's make sure.

When you deploy, do you have a WEB-INF\classes folder? And in it do you see your db.properties file? If not I suppose that would be indicative of your classpath issue.

I just had the same problem and I found that the problem was the properties name were not matching the ones in the servlet.xml I had "database.driver" on the servlet and "database.driverClassName" on the properties file. Maybe this is your problem too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top