org.springframework.beans.factory.BeanCreationException:以及org.springframework.beans.BeanInstantiationException

时间:2012-11-30 10:01:24

标签: spring

我试着在春天做登录和一些数据库操作(插入,删除),也帮助jsp.I对spring来说是全新的。我得到了以下错误,比如bean创建异常。我已经将dvd定义为servlet名称在web.xml中。所以我的dvd_servlet.xml文件如下所示

<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>

我的LoginController就像

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"); 
    }

我的错误如下:

org.springframework.beans.factory.BeanCreationException:创建名称为&#39; LoginController&#39;的bean时出错在ServletContext资源[/WEB-INF/dvd-servlet.xml]中定义:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[LoginController]:构造函数抛出异常;嵌套异常是java.lang.IllegalArgumentException:Property&#39; dataSource&#39;是必须的     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(支持:AbstractAutowireCapableBeanFactory.java):965)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(支持:AbstractAutowireCapableBeanFactory.java):911)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(支持:AbstractAutowireCapableBeanFactory.java):485)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(支持:AbstractAutowireCapableBeanFactory.java):456)     org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(支持:AbstractBeanFactory.java):291)     org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(支持:DefaultSingletonBeanRegistry.java):222)     org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(支持:AbstractBeanFactory.java):288)     org.springframework.beans.factory.support.AbstractBeanFactory.getBean(支持:AbstractBeanFactory.java):190)     org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(支持: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.beans.BeanInstantiationException:无法实例化bean类[LoginController]:构造函数抛出异常;嵌套异常是java.lang.IllegalArgumentException:Property&#39; dataSource&#39;是必须的     org.springframework.beans.BeanUtils.instantiateClass(豆:BeanUtils.java):141)     org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(支持:SimpleInstantiationStrategy.java):74)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(支持:AbstractAutowireCapableBeanFactory.java):958)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(支持:AbstractAutowireCapableBeanFactory.java):911)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(支持:AbstractAutowireCapableBeanFactory.java):485)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(支持:AbstractAutowireCapableBeanFactory.java):456)     org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(支持:AbstractBeanFactory.java):291)     org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(支持:DefaultSingletonBeanRegistry.java):222)     org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(支持:AbstractBeanFactory.java):288)     org.springframework.beans.factory.support.AbstractBeanFactory.getBean(支持:AbstractBeanFactory.java):190)     org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(支持: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)

根本原因

java.lang.IllegalArgumentException:Property&#39; dataSource&#39;是必须的     org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(支持:JdbcAccessor.java):134)     。org.springframework.jdbc.core.JdbcTemplate(芯:JdbcTemplate.java):164)     的LoginController(LoginController.java:29)     sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)     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(豆:BeanUtils.java):126)     org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(支持:SimpleInstantiationStrategy.java):74)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(支持:AbstractAutowireCapableBeanFactory.java):958)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(支持:AbstractAutowireCapableBeanFactory.java):911)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(支持:AbstractAutowireCapableBeanFactory.java):485)     org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(支持:AbstractAutowireCapableBeanFactory.java):456)     org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(支持:AbstractBeanFactory.java):291)     org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(支持:DefaultSingletonBeanRegistry.java):222)     org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(支持:AbstractBeanFactory.java):288)     org.springframework.beans.factory.support.AbstractBeanFactory.getBean(支持:AbstractBeanFactory.java):190)     org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(支持: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)

请帮我清除这个!!!

1 个答案:

答案 0 :(得分:1)

我在这里看到两个问题:

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

LoginController真的是完全限定的类名吗?它应该像com.cy.LoginController

其次,Spring声明LoginController的构造函数需要属性dataSource。看起来LoginController需要构造函数注入dataSource,你正在做的是setter注入,所以试试这个:

而不是<property name="dataSource" ref="dataSource/>,写道: <constructor-arg ref="dataSource"/>