Question

I have a database project which is using spring. For that I have two important files in src/META-INF/spring/(database project)

The first one is the cmn-dao-spring.xml . The other one is the database.properties.

In my tomcat webapp project I am able to load with that code all the needed context-files:

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
              classpath*:/META-INF/spring/*.xml  
    </param-value>
  </context-param>

The problem is that the database.properties is not loaded. If I change the xml to this:

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
              classpath*:/META-INF/spring/*  
    </param-value>
  </context-param>

I get the Exception:

Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.

because the properties is no valid xml. The startup of my tomcat fails.

How can I include the database.properties from my cmn-dao project in my webapp?

EDIT

That is my cmn-dao.xml:

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

    <tx:annotation-driven />
    <context:annotation-config />
    <!-- DATABASE CONFIGURATION -->

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>META-INF/spring/database.properties</value>
        </property>
    </bean>

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

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- BEAN DEFINITIONS -->

    <bean id="scoreMapper" class="de.bc.qz.dao.mapper.ScoreMapper"
        autowire="byName">
        <constructor-arg value="s." />
    </bean>
    <bean id="scoreExtractor" class="de.bc.qz.dao.extractor.ScoreExtractor"
        autowire="byName">
    </bean>
    <bean id="questionMapper" class="de.bc.qz.dao.mapper.QuestionMapper"
        autowire="byName">
        <constructor-arg value="q." />
    </bean>
    <bean id="complaintMapper" class="de.bc.qz.dao.mapper.ComplaintMapper"
        autowire="byName">
        <constructor-arg value="c." />
    </bean>

    <bean id="scoreDao" class="de.bc.qz.dao.ScoreDao" autowire="byName">
        <property name="dataSource" ref="dataSource" />
        <property name="LAUSFT">
            <value>
                SELECT * FROM (
                SELECT s.*, @rank
                := @rank + 1 rank
                FROM
                quiz.score s, (SELECT @rank := 0) init
                ORDER BY points DESC
                ) s
                WHERE rank BETWEEN ? AND ?
                ORDER BY rank;
        </value>
        </property>
        <property name="LUS">
            <value>
                SELECT id
                FROM quiz.score
                WHERE username = ? AND uuid = ?;
        </value>
        </property>
    </bean>
    <bean id="complaintDao" class="de.bc.qz.dao.ComplaintDao"
        autowire="byName">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <bean id="questionDao" class="de.bc.qz.dao.QuestionDao" autowire="byName">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

The JUnit's for cmn-dao works absolut correct and the placeholder works too. Inside tomcat project I have added the related projects via Deployment Assembly.

Thx for your help Stefan

Was it helpful?

Solution

use spring util to read properties file.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:util="http://www.springframework.org/schema/util"
  xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">

    <context:annotation-config />

    <util:properties id="db_config" location="classpath:db_config.properties"></util:properties>


</beans>

user spring new '@value' annotation to get the property file key=value i.e

example : db_config.properties contains db_user_name = uttesh db_password = password

to get "db.user.name" property value use below code

@Value("#{db_config[db_user_name]}")
private String dbUsername;

OTHER TIPS

contextCongifLocation is for spring configuration files only.

use this in your spring config (xml) file for loading properties:

<bean id="props" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath:META-INF/spring/database.properties"/>
</bean>

or

<context:property-placeholder location="classpath:META-INF/spring/database.properties" />

Try to use the util namespace instead, it allows to load several property files and separate them into property groups:

<util:properties id="application" location="classpath:application.properties"/>

To use the properties:

<bean id="postgresDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="#{application.driverClassName}"/>
    <property name="url" value="#{application.url}"/>
    <property name="username" value="#{application.username}"/>
    <property name="password" value="#{application.password}"/>
</bean>

This is the properties file example:

driverClassName=org.postgresql.Driver
url=jdbc:postgresql://localhost:5432/somedatabase
username=dummy
password=dummy

Change your context file to something like that :

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
              classpath*:/META-INF/spring/*.properties  
    </param-value>
</context-param> 

Use contextConfigLocation in web.xml to read your main spring config xml file.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
              classpath:/META-INF/spring/spring-app-context.xml 
    </param-value>
  </context-param>

import cmn-dao.xml in spring-app-context.xml

<import resource="classpath:/META-INF/spring/cmn-dao.xml" />

Now use PropertyPlaceholderConfigurer to read database.properties in cmn-dao.xml

<bean id="props" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath:META-INF/spring/database.properties"/>
</bean>

This is how you load property files into your spring context:

<?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:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
</beans>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top