[英]Change jdbc.properties with context.xml
I make Spring webapp and I use jdbc.properties files from DB. 我制作Spring webapp,并使用数据库中的jdbc.properties文件。 In applicationContext.xml I have this
在applicationContext.xml中,我有这个
<context:property-placeholder location="classpath:cfg/properties/jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
Now, I want to change place from my properties from my DB , now I have context.xml file in tomcat and also context.xml in META-INF in webapp . 现在,我想从数据库的属性中更改位置,现在我在tomcat中有context.xml文件,在webapp中的META-INF中也有context.xml。 in context.xml I have :
在context.xml中,我有:
<?xml version='1.0' encoding='utf-8'?>
<Context path="/webapp" docBase="../webapp/webapp.war" displayName="webapp">
<Environment name="jdbc.driverClassName" override="false" type="java.lang.String" value="org.postgresql.Driver"/>
<Environment name="jdbc.dialect" override="false" type="java.lang.String" value="..."/>
<Environment name="jdbc.databaseurl" override="false" type="java.lang.String" value="..."/>
<Environment name="jdbc.username" override="false" type="java.lang.String" value=""/>
<Environment name="jdbc.password" override="false" type="java.lang.String" value=""/>
</Context>
What I need to change in applicationContext to webapp looking properties from context.xml , or better how I can put in jdbc.properties values from tomcat context.xml file? 我需要将applicationContext中的内容更改为从context.xml中查找Webapp的属性,或者更好地如何从tomcat context.xml文件中放入jdbc.properties值?
And that's where JNDI comes in... In your Tomcat's conf directory, context.xml
file add: 这就是JNDI进入的地方...在Tomcat的conf目录中,
context.xml
文件添加:
<Resource name="jdbc/your_app_ds"
auth="Container"
type="javax.sql.DataSource"
username="username"
password="password"
driverClassName="org.postgresql.Driver"
maxActive="100"
maxIdle="10"
validationQuery="select 1"
minEvictableIdleTimeMillis="300000"
timeBetweenEvictionRunsMillis="100000"
testWhileIdle="true"
url="db_url"/>
Then in your Spring file all you need to do is to define JNDI entry like this: 然后在您的Spring文件中,您所需要做的就是定义JNDI条目,如下所示:
<jee:jndi-lookup id="dataSource"
jndi-name="jdbc/your_app_ds"
expected-type="javax.sql.DataSource"/>
This way Spring will create javax.sql.DataSource
implementation instance and you'll get a hold of it via dataSource
id. 这样,Spring将创建
javax.sql.DataSource
实现实例,您将通过dataSource
id来获取它。
Just do not forget to specify schema location for jee
prefix: 只是不要忘记为
jee
前缀指定架构位置:
xmlns:jee="http://www.springframework.org/schema/jee"
and: 和:
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"
You can have a propery holder bean like 你可以有一个属性持有人豆像
<!-- Load values for configuration placeholders, evaluated at startup only -->
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="location" value="file:${user.dir}/jdbc.properties" />
</bean>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.