title: Spring Web App - Simple JDBC Template Advice?
body: <p>Quick question based on a spring web app i am creating. </p>
  
  <p>How do you go about setting up the context of the application so that you do not need to set the datasource parameters for simpleJDBC all the time and can call getSimpleJDBCTemplate().queryfor.... and it be set up with the datasource. </p>
  
  <p>This is how i currently have it and it seems to go against inversion of control that spring is meant to provide as this is in every dao! </p>
  
  <pre><code> ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:ApplicationContext.xml");
      DataSource dataSource = (DataSource) ac.getBean("dataSource");
      SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
  </code></pre>
  
  <p>ApplicationContext</p>
  
  <pre><code>&lt;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"
            xsi:schemaLocation="
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"&gt;
  
      &lt;bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; 
  &lt;property name="location" value="classpath:properties.properties"/&gt;
  &lt;/bean&gt;
  
      &lt;bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"&gt;
          &lt;property name="driverClassName" value="${jdbc.driverClassName}"/&gt;
          &lt;property name="url" value="${jdbc.url}"/&gt;
          &lt;property name="username" value="${username}"/&gt;
          &lt;property name="password" value="${password}"/&gt;
      &lt;/bean&gt;
  
   &lt;bean name="SimpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"&gt;
      &lt;constructor-arg&gt;&lt;ref bean="dataSource"/&gt;&lt;/constructor-arg&gt;
  &lt;/bean&gt;
  
  
        &lt;context:annotation-config/&gt;
     &lt;/beans&gt;
  </code></pre>
  
  <p>Latest Stack Trace from Tomcat log</p>
  
  <pre><code>13-Jan-2011 20:15:18 com.sun.jersey.api.core.PackagesResourceConfig init
  INFO: Scanning for root resource and provider classes in the packages:
    ptc.jersey.spring
  13-Jan-2011 20:15:18 com.sun.jersey.api.core.ScanningResourceConfig logClasses
  INFO: Root resource classes found:
    class ptc.jersey.spring.resources.LoginResource
  13-Jan-2011 20:15:18 com.sun.jersey.api.core.ScanningResourceConfig init
  INFO: No provider classes found.
  13-Jan-2011 20:15:19 com.sun.jersey.spi.spring.container.servlet.SpringServlet getContext
  INFO: Using default applicationContext
  13-Jan-2011 20:15:19 com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
  INFO: Initiating Jersey application, version 'Jersey: 1.4 09/11/2010 10:30 PM'
  13-Jan-2011 20:15:21 com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
  SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
  java.lang.NullPointerException
      at ptc.jersey.spring.daoImpl.UserDaoImpl.getUser(UserDaoImpl.java:43)
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;listener&gt;
      &lt;listener-class&gt;
          org.springframework.web.context.ContextLoaderListener
      &lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;classpath:ApplicationContext.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;Jersey Spring Web Application&lt;/servlet-name&gt;
          &lt;servlet-class&gt;com.sun.jersey.spi.spring.container.servlet.SpringServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
    &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
    &lt;param-value&gt;ptc.jersey.spring&lt;/param-value&gt;
  &lt;/init-param&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Jersey Spring Web Application&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/resources/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Any help would be great 
  Thanks 
  Chris</p>