无法在Spring MVC中使用控制器自动连接Dao

时间:2018-07-20 06:17:28

标签: java

****您好,我正在尝试使用Spring MVC创建简单的登录应用程序。在运行我的应用程序时,出现以下异常,即无法自动连接Dao。****

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.dao.loginDao com.controller.HelloController.dao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dao' defined in ServletContext resource [/WEB-INF/spring-web-servlet.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframeenter code herework/dao/DataAccessException

下面是我的控制器类

package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.beans.Customer;
import com.dao.loginDao;;

@Controller
public class HelloController {

    @Autowired
     private loginDao loginDao;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
            //  System.out.println("***********8888 controller *************************");
        model.addAttribute("message", "Hi you are successfully logged in");
        return "hello";

    }
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String varifyLogin(ModelMap model , @RequestParam String userId , @RequestParam String password) {

        Customer customer = loginDao.login(userId, password);
        System.out.println("***********8888 controller *************************");
        model.addAttribute("message", "welcome to Spring MVC");
        return "welcome";
    }
}

请在下面查看道课:-

package com.dao;
import com.beans.*;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;  
import org.springframework.stereotype.Component;

@Component
public class loginDao {

    JdbcTemplate template;  

    public void setTemplate(JdbcTemplate template) {  
        this.template = template;}

    public Customer login(String userId, String password){
        String sql="select * from Customer where CUSTOMER_ID=?";  
        Customer customer =  template.queryForObject(sql, new Object[]{userId},new BeanPropertyRowMapper<Customer>(Customer.class));  
        if(customer != null && customer.getPassword().equals(password))
        {
            return customer;
        }
        return null;
        //template.
    }
}

下面是我的spring.xml,其中提到了bean的定义:-

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <context:component-scan base-package="com.controller" />
    <context:component-scan base-package="com.dao"/>


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

    <mvc:annotation-driven />

     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" autowire="byName">
        <property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"></property>
        <property name="username" value="SHAKTI_ELE"></property>
        <property name="password" value="root"></property>
    </bean>

<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">  
<property name="dataSource" ref="ds"></property>  
</bean>  

<bean id="dao" class="com.dao.loginDao">  
<property name="template" ref="jt"></property>  
</bean>  
</beans>

1 个答案:

答案 0 :(得分:0)

在自动连线中使用限定词:-

@Autowired
@Qualifier("dao")
private loginDao loginDao;