[英]include database.properties from spring project into web.xml
I have a database project which is using spring
. 我有一个使用spring
的数据库项目。 For that I have two important files in src/META-INF/spring/
(database project) 为此我在src/META-INF/spring/
(数据库项目)中有两个重要文件
The first one is the cmn-dao-spring.xml . 第一个是cmn-dao-spring.xml。 The other one is the database.properties. 另一个是database.properties。
In my tomcat webapp project I am able to load with that code all the needed context-files: 在我的tomcat webapp项目中,我能够使用该代码加载所有需要的上下文文件:
<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. 问题是未加载database.properties
。 If I change the xml to this: 如果我将xml更改为:
<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
. 因为properties
没有有效的xml
。 The startup of my tomcat fails. 我的tomcat启动失败了。
How can I include the database.properties
from my cmn-dao project in my webapp? 如何在我的webapp中包含my cmn-dao项目中的database.properties
?
EDIT 编辑
That is my cmn-dao.xml: 那是我的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. 对于cmn-dao,JUnit的工作方式绝对正确,占位符也可以正常工作。 Inside tomcat project I have added the related projects via Deployment Assembly
. 在tomcat项目中,我通过Deployment Assembly
添加了相关项目。
Thx for your help Stefan 谢谢你的帮助Stefan
contextCongifLocation
is for spring configuration files only. contextCongifLocation
仅适用于spring配置文件。
use this in your spring config (xml) file for loading properties: 在spring config(xml)文件中使用它来加载属性:
<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命名空间 ,它允许加载多个属性文件并将它们分成属性组:
<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. 使用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>
import cmn-dao.xml in spring-app-context.xml 在spring-app-context.xml中导入cmn-dao.xml
<import resource="classpath:/META-INF/spring/cmn-dao.xml" />
Now use PropertyPlaceholderConfigurer to read database.properties in 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>
This is how you load property files into your spring context: 这是将属性文件加载到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>
use spring util to read properties file. 使用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>
user spring new '@value' annotation to get the property file key=value ie 用户弹出新的'@value'注释以获取属性文件key = value ie
example : db_config.properties contains db_user_name = uttesh db_password = password 示例:db_config.properties包含db_user_name = uttesh db_password = password
to get "db.user.name" property value use below code 获取“db.user.name”属性值使用下面的代码
@Value("#{db_config[db_user_name]}")
private String dbUsername;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.