File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes data source defined but not found.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "data source defined but not found.." Watch "data source defined but not found.." New topic

data source defined but not found..

kunal green

Joined: Dec 31, 2009
Posts: 26

hi i am writing a small smple application in spring having the following details:

package com.vaannila.domain;

import java.sql.Types;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class UsertableDAOImpl implements UsertableDAO{

JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;

//private DataFieldMaxValueIncrementer dataFieldMaxValueIncrementer;
public DataSource dataSource;
public DataSource getDataSource() {
return dataSource;

public void addUser(Usertable usertable) {
// TODO Auto-generated method stub
//get next autoincrement value
int i=0;
Integer id = new Integer(++i);//dataFieldMaxValueIncrementer.nextIntValue();
String sql = "insert into Usertable (userid, name, password,gender,country,aboutyou,mailinglist,community) values (?,?,?)";
Object args []= new Object[] { id.toString(), usertable.getName(), usertable.getPassword(),usertable.getGender(),usertable.getCountry(),usertable.getMailinglist(),usertable.getCommunity(),usertable.getAboutyou() };
int types[] = new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR ,Types.VARCHAR, Types.VARCHAR, Types.VARCHAR ,Types.VARCHAR,Types.VARCHAR};
//DataSource da=new Da
//JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
jdbcTemplate.update(sql, args, types);//Exception line----------------------------------------------

public void deleteUser(Usertable usertable) {
// TODO Auto-generated method stub


public List<Usertable> getUsers(Usertable usertable) {
// TODO Auto-generated method stub
return null;

public void updateUser(Usertable user) {
// TODO Auto-generated method stub

// public void setDataFieldMaxValueIncrementer(
// DataFieldMaxValueIncrementer dataFieldMaxValueIncrementer) {
// this.dataFieldMaxValueIncrementer = dataFieldMaxValueIncrementer;
// }

public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<bean id="messageSource" class="" p:basename="messeges" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

<bean id="userService" class="com.vaannila.service.UserServiceImpl" >


<bean name="/userRegistration.htm" class="com.vaannila.web.UserController" p:userService-ref="userService" p:formView="userForm" p:successView="userSuccess" p:validator-ref="userValidator" />

<bean id="userValidator" class="com.vaannila.validator.UserValidator" />

<bean id="usertableDAO" class="com.vaannila.domain.UsertableDAOImpl" >
<property name="dataSource" ref="dataSource"/>
<!-- <property name="jdbcTemplate" ref="jdbcTemplate"/>-->
<!-- <property name="dataFieldMaxValueIncrementer"> <ref bean="dataFieldMaxValueIncrementer" />-->
<!-- </property>-->

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<ref bean="dataSource" />

<!-- <bean id="dataFieldMaxValueIncrementer" class="">-->
<!-- <constructor-arg>-->
<!-- <ref bean="dataSource" />-->
<!-- </constructor-arg>-->
<!-- <constructor-arg><value>book_id_seq</value> </constructor-arg>-->
<!-- </bean>-->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbcracle:thin:@localhost:1521:XE" />
<property name="username" value="kunal" />
<property name="password" value="kunal" />


SEVERE: Servlet.service() for servlet dispatcher threw exception
at com.vaannila.domain.UsertableDAOImpl.addUser(
at com.vaannila.service.UserServiceImpl.add(
at com.vaannila.web.UserController.onSubmit(
at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(
at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(
at org.springframework.web.servlet.DispatcherServlet.doDispatch(
at org.springframework.web.servlet.DispatcherServlet.doService(
at org.springframework.web.servlet.FrameworkServlet.processRequest(
at org.springframework.web.servlet.FrameworkServlet.doPost(
at javax.servlet.http.HttpServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.valves.ErrorReportValve.invoke(
at org.apache.catalina.core.StandardEngineValve.invoke(
at org.ap

please help
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 510

please use code tags while posting to a forum, without code tags, it is very difficult to read, can you please edit this one, which is the 34 line that is throwing the error?

Regards, Prasad
SCJP 5 (93%)
kunal green

Joined: Dec 31, 2009
Posts: 26

is the line which is throwing exception is
jdbcTemplate.update(sql, args, types);//Exception line----------------------------------------------

it appears that datasource is not injected properly but i verified it from the dispatcher-servlet.xml i might be wrong but it appears correct to me.
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17250

kunal green wrote:is the line which is throwing exception is
jdbcTemplate.update(sql, args, types);//Exception line----------------------------------------------

it appears that datasource is not injected properly but i verified it from the dispatcher-servlet.xml i might be wrong but it appears correct to me.

That might be your problem. If you define the datasource in your dispatcher servlet.xml and it gets created by the DispatcherServlet, but you other classes is in the ApplicationContext created by ContextLoaderListener, then you will not be able to inject the dataSource into those classes.

Basically in a Spring MVC app you create two ApplicationContexts. One for the middle tier, your services/repositories/dataSource/transactionManager. The other ApplicationContext from the dispatcherServlet should only contain Web Layer beans like Controllers, View Resolvers and MappingHandlers

So with the two app contexts. the web layer can see the beans in the middle tier, but not the other way around.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
jQuery in Action, 2nd edition
subject: data source defined but not found..