-1

I tried to do login and some database manipulations(insert,delete) in spring and also help with jsp.I am totally new to spring.I am getting the following error like bean creation exception.I have defined dvd as servlet name in web.xml. So my dvd_servlet.xml file is as like below

<beans.................>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/DVD"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
</bean>

<bean name="DvdController" class="DvdController" >
    <property name="dataSource" ref="dataSource"/>
</bean>
<bean name="LoginController" class="LoginController" >
    <property name="dataSource" ref="dataSource"/>
</bean>
<bean name="AccountCreationController" class="AccountCreationController" >
    <property name="dataSource" ref="dataSource"/>
</bean>
</beans>

my LoginController is like

public class LoginController implements Controller   {

     int check;
    private DataSource dataSource;
     public void setDataSource(DataSource dataSource) {
    this.dataSource=dataSource;
    }
    public DataSource getDataSource() {
    return dataSource;
    }

    JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
    @RequestMapping(value="/Login",method=RequestMethod.POST)
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {                       
                String userName = request.getParameter("userName");
            String password=request.getParameter("password");
            try {
            String selectQuery=("SELECT * FROM USERDETAILS WHERE NAME='"+userName+"'AND PASSWORD='"+password+"'");
                check=jdbcTemplate.update(selectQuery,new Object[]{});  
                System.out.println(check);
                if(check!=0) {
                response.sendRedirect("DvdController");

                }
            }

            catch(IOException e) {
            e.printStackTrace();            
            }

            return new ModelAndView("failure"); 
    }

My error is like as below:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'LoginController' defined in ServletContext resource [/WEB-INF/dvd-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [LoginController]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(support:AbstractAutowireCapableBeanFactory.java):965) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(support:AbstractAutowireCapableBeanFactory.java):911) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(support:AbstractAutowireCapableBeanFactory.java):485) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(support:AbstractAutowireCapableBeanFactory.java):456) org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(support:AbstractBeanFactory.java):291) org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(support:DefaultSingletonBeanRegistry.java):222) org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(support:AbstractBeanFactory.java):288) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(support:AbstractBeanFactory.java):190) org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(support:DefaultListableBeanFactory.java):580) org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)

root cause

org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [LoginController]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required org.springframework.beans.BeanUtils.instantiateClass(beans:BeanUtils.java):141) org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(support:SimpleInstantiationStrategy.java):74) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(support:AbstractAutowireCapableBeanFactory.java):958) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(support:AbstractAutowireCapableBeanFactory.java):911) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(support:AbstractAutowireCapableBeanFactory.java):485) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(support:AbstractAutowireCapableBeanFactory.java):456) org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(support:AbstractBeanFactory.java):291) org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(support:DefaultSingletonBeanRegistry.java):222) org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(support:AbstractBeanFactory.java):288) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(support:AbstractBeanFactory.java):190) org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(support:DefaultListableBeanFactory.java):580) org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)

root cause

java.lang.IllegalArgumentException: Property 'dataSource' is required org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(support:JdbcAccessor.java):134) org.springframework.jdbc.core.JdbcTemplate.(core:JdbcTemplate.java):164) LoginController.(LoginController.java:29) sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) java.lang.reflect.Constructor.newInstance(Constructor.java:513) org.springframework.beans.BeanUtils.instantiateClass(beans:BeanUtils.java):126) org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(support:SimpleInstantiationStrategy.java):74) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(support:AbstractAutowireCapableBeanFactory.java):958) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(support:AbstractAutowireCapableBeanFactory.java):911) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(support:AbstractAutowireCapableBeanFactory.java):485) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(support:AbstractAutowireCapableBeanFactory.java):456) org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(support:AbstractBeanFactory.java):291) org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(support:DefaultSingletonBeanRegistry.java):222) org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(support:AbstractBeanFactory.java):288) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(support:AbstractBeanFactory.java):190) org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(support:DefaultListableBeanFactory.java):580) org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458) org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)

please help me to clear this!!!

thilo
  • 439
  • 1
  • 5
  • 6
  • Where is the "dataSource" bean declared? If it *is* declared somewhere then check that there are no exceptions regarding the creation of "datasource" bean in the log. – Boris Treukhov Nov 30 '12 at 10:16
  • I have attached my LoginController progarm for your reference.Review it and make me to clear. – thilo Nov 30 '12 at 10:29
  • if you have a DataSource bean that means that there is another exception - scroll down the log and check it's not truncated. – Boris Treukhov Nov 30 '12 at 10:32
  • 1
    What is `Controller` if it is your class maybe you have declared a dataSource property there and annotated it with `@Required` instead of `@Autowired` – Boris Treukhov Nov 30 '12 at 10:37
  • ya thank you Boris Treukhov.Is it any study tutorial for spring for this concept – thilo Nov 30 '12 at 10:50
  • There are two types of wiring - wiring with xml configuration and wiring with java configuration, they can be mixed together without problems, but typically only type of configuration per bean is enough - these two types of wiring are described in the most of Spring books for example in `Spring In Action`. If you want to know the differences between the java configuration annotations you can check my answer http://stackoverflow.com/questions/12229282/difference-between-spring-annotations - the canonical SpringSource documentation and javadocs are primary sources there. – Boris Treukhov Nov 30 '12 at 11:03
  • as for `@Required` annotation it's just checks that the wiring has been done, for example in xml configuration. It does not wire anything on its own http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-required-annotation – Boris Treukhov Nov 30 '12 at 11:08
  • so it looks like you have declared datasource property in your controller which shadows the one which is declared in the base class – Boris Treukhov Nov 30 '12 at 11:10

1 Answers1

1

I see two issues here:

<bean name="LoginController" class="LoginController" >
    <property name="dataSource" ref="dataSource"/>
</bean>

Is LoginController really the full qualified class name? It should be something like com.cy.LoginController

Second, Spring states that the constructor of the LoginController needs the property dataSource. It looks like the LoginController needs constructor injection of the dataSource, what you are doing is setter injection, so try this:

Instead of <property name="dataSource" ref="dataSource/>, write: <constructor-arg ref="dataSource"/>

ri5b6
  • 370
  • 1
  • 10
  • ya I created LoginController under classes folder so i dont need any package structure.I did not defined dataSource as constructor argument.its just like a variable.I added my Logincontroller in question.please review that program and make me to clear – thilo Nov 30 '12 at 10:28
  • You should also post the Controller interface, so we can see if there are any annotations, as Boris stated in the comment above. – ri5b6 Nov 30 '12 at 11:44