0

First of all this is the first time that I am trying spring. I have a problem while connecting to a database using spring's JDBC template

Here's my code for AddEmployee Servlet:

package com.lftechnology.spring.jdbctemplate.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lftechnology.spring.jdbctemplate.model.dao.EmployeeDAO;
import com.lftechnology.spring.jdbctemplate.model.dao.EmployeeDAOImpl;
import com.lftechnology.spring.jdbctemplate.model.dao.mysql.EmployeeMySql;
import com.lftechnology.spring.jdbctemplate.model.dto.Employee;

@WebServlet("/AddEmployee")
public class AddEmployee extends HttpServlet 
{
    private static final long serialVersionUID = 1L;


    Employee employee;


    public AddEmployee() {
        super();

    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {

        RequestDispatcher dispatcher=request.getRequestDispatcher("addemployee.jsp");
        dispatcher.forward(request, response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {

        ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");

        EmployeeDAO employeeDAO= (EmployeeDAO) context.getBean("employeeDAO");

       employee=new Employee();
       String name=(String) request.getAttribute("name");
       String designation=(String) request.getAttribute("designation");

       employee.setName(name);
       employee.setDesignation(designation);

       employeeDAO.insert(employee);

       RequestDispatcher dispatcher =request.getRequestDispatcher("success.jsp");
       dispatcher.forward(request,response);

    }

}

Corresponding class named EmployeeMySql for JDBC connection is

package com.lftechnology.spring.jdbctemplate.model.dao.mysql;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.lftechnology.spring.jdbctemplate.model.dao.EmployeeDAO;
import com.lftechnology.spring.jdbctemplate.model.dto.Employee;

public class EmployeeMySql extends JdbcTemplate implements EmployeeDAO
{

    private DataSource dataSource;




    public void setDataSource(DataSource dataSource) 
    {
        System.out.println("Datasource here"+dataSource.toString());
        try {
            System.out.println("Datasource here"+dataSource.getLoginTimeout());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        this.dataSource = dataSource;
    }

    public void insert(Employee employee)
    {
        String query_sql;
        query_sql="INSERT INTO EMPLOYEE (name,designation) VALUES (?,?,?)";

        update(query_sql,new Object[]{employee.getName(),employee.getDesignation()});
    }

    @Override
    public Employee retrieveAllData() {
        // TODO Auto-generated method stub
        return null;
    }



}

Similarly my spring bean configuration is spring.xml

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



<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

                           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

                           http://www.springframework.org/schema/tx

                           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd

                           http://www.springframework.org/schema/aop

                           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">


<bean id="dataSourcee" 
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

<bean id="employeeDAO" class="com.lftechnology.spring.jdbctemplate.model.dao.mysql.EmployeeMySql">
   <property name="dataSource" ref="dataSourcee"/>
</bean>
<!-- import resource="Spring-Datasource.xml" />
    <import resource="Spring-Customer.xml" /-->


</beans>

However i always end up with one error which i cant figure out at all.Error that i get is

HTTP Status 500 - Error creating bean with name 'employeeDAO' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required

type Exception report

message Error creating bean with name 'employeeDAO' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDAO' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:605)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
    org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    com.lftechnology.spring.jdbctemplate.controller.AddEmployee.doPost(AddEmployee.java:46)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

java.lang.IllegalArgumentException: Property 'dataSource' is required
    org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:134)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:605)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
    org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    com.lftechnology.spring.jdbctemplate.controller.AddEmployee.doPost(AddEmployee.java:46)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

Could someone tell me what the error really is? Help would be much appreciated.

2
  • In your setDataSource method, make a call to super.setDataSource. You have your own dataSource variable, however, the base class also does some initialization with the given dataSource instance and that is what is missed out. Commented Apr 29, 2013 at 17:53
  • I will convert this comment into an answer. Commented Apr 29, 2013 at 17:58

1 Answer 1

2

In your setDataSource method, make a call to super.setDataSource. You have your own dataSource variable, however, the base class also does some initialization with the given dataSource instance and that is what is missed out.

Sign up to request clarification or add additional context in comments.

1 Comment

In addition: Instead of calling the setDataSource property, remove the code completely for this and use the constructor of the base class then in spring use the <constructor-arg ref="dataSourcee"/> instead of Property tag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.