簡體   English   中英

將spring.properties從spring項目包含到web.xml中

[英]include database.properties from spring project into 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啟動失敗了。

如何在我的webapp中包含my 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添加了相關項目。

謝謝你的幫助Stefan

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" />

嘗試使用util命名空間 ,它允許加載多個屬性文件並將它們分成屬性組:

<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

將您的上下文文件更改為:

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

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

這是將屬性文件加載到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>

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

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

獲取“db.user.name”屬性值使用下面的代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM