一尘不染

将spring项目中的database.properties包含到web.xml中

tomcat

我有一个正在使用的数据库项目spring。为此,我在src/META-INF/spring/(数据库项目)中有两个重要文件

第一个是cmn-dao-spring.xml。另一个是database.properties。

在我的tomcat webapp项目中,我能够使用该代码加载所有需要的上下文文件:

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

问题是database.properties未加载。如果我将xml更改为此:

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

我得到异常:

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

因为properties无效xml。我的tomcat启动失败。

如何database.properties在我的Webapp中包含来自cmn-dao项目的?

编辑

那就是我的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>

用于cmn-dao的JUnit绝对正确,占位符也起作用。在tomcat项目中,我通过添加了相关项目Deployment Assembly

感谢您的帮助Stefan


阅读 398

收藏
2020-06-16

共1个答案

一尘不染

使用spring util读取属性文件。

<?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>

用户使用新的’@value’批注获取属性文件key = value,即

示例:db_config.properties包含db_user_name = uttesh db_password =密码

要获取“ db.user.name”属性值,请使用以下代码

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