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

时间:2013-12-14 11:33:02

标签: java spring tomcat web.xml

我有一个使用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启动失败了。

如何在我的网络应用程序中包含我的cmn-dao项目中的database.properties

修改

那是我的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添加了相关项目。

谢谢你的帮助 斯蒂芬

6 个答案:

答案 0 :(得分:1)

contextCongifLocation仅适用于spring配置文件。

在spring config(xml)文件中使用它来加载属性:

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

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

答案 1 :(得分:1)

尝试使用util namespace,它允许加载多个属性文件并将它们分成属性组:

<util:properties id="application" location="classpath:application.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>

这是属性文件示例:

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

答案 2 :(得分:0)

将您的上下文文件更改为:

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

答案 3 :(得分:0)

使用web.xml中的contextConfigLocation读取主spring config xml文件。

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

在spring-app-context.xml中导入cmn-dao.xml

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

现在使用PropertyPlaceholderConfigurer读取cmn-dao.xml中的database.properties

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

答案 4 :(得分:0)

这是将属性文件加载到spring上下文中的方法:

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

答案 5 :(得分:0)

使用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;