简体   繁体   中英

Change jdbc.properties with context.xml

I make Spring webapp and I use jdbc.properties files from DB. In applicationContext.xml I have this

<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 . in context.xml I have :

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

And that's where JNDI comes in... In your Tomcat's conf directory, context.xml file add:

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

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

Just do not forget to specify schema location for jee prefix:

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM