java - Change jdbc.properties with context.xml -


i make spring webapp , use jdbc.properties files db. in applicationcontext.xml have

<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, want change place properties db , have context.xml file in tomcat , context.xml in meta-inf in webapp . in context.xml 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 need change in applicationcontext webapp looking properties context.xml , or better how can put in jdbc.properties values tomcat context.xml file?

and that's jndi comes in... in 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 spring file need define jndi entry this:

<jee:jndi-lookup id="datasource"                  jndi-name="jdbc/your_app_ds"                  expected-type="javax.sql.datasource"/> 

this way spring create javax.sql.datasource implementation instance , you'll hold of via datasource id.

just not forget specify schema location 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" 

Comments