将 spring 项目中的 database.properties 包含到 web.xml 中 [英] include database.properties from spring project into web.xml
问题描述
我有一个使用 spring
的数据库项目.为此,我在 src/META-INF/spring/
(数据库项目)
I have a database project which is using spring
. For that I have two important files in src/META-INF/spring/
(database project)
第一个是 cmn-dao-spring.xml .另一个是database.properties.
The first one is the cmn-dao-spring.xml . The other one is the database.properties.
在我的 tomcat webapp 项目中,我能够使用该代码加载所有需要的上下文文件:
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>
问题是 database.properties
没有加载.如果我将 xml 更改为:
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>
我得到异常:
Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
因为 properties
不是有效的 xml
.我的tomcat启动失败.
because the properties
is no valid xml
.
The startup of my tomcat fails.
如何将我的 cmn-dao 项目中的 database.properties
包含在我的 web 应用程序中?
How can I include the database.properties
from my cmn-dao project in my webapp?
编辑
那是我的 cmn-dao.xml:
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>
用于 cmn-dao 的 JUnit 工作绝对正确,占位符也工作.在 tomcat 项目中,我通过 Deployment Assembly
添加了相关项目.
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
.
感谢您的帮助斯蒂芬
推荐答案
使用 spring util 读取属性文件.
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>
用户 spring 新的 '@value' 注释以获取属性文件 key=value 即
user spring new '@value' annotation to get the property file key=value i.e
示例:db_config.properties 包含db_user_name = utteshdb_password = 密码
example : db_config.properties contains db_user_name = uttesh db_password = password
使用下面的代码获取db.user.name"属性值
to get "db.user.name" property value use below code
@Value("#{db_config[db_user_name]}")
private String dbUsername;
这篇关于将 spring 项目中的 database.properties 包含到 web.xml 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!