没有'com.springmvc.dao.UserDAO'类型的限定bean可用:预计至少有1个bean可以作为autowire candidate

时间:2017-03-04 12:00:55

标签: spring

我收到错误,因为没有'com.springmvc.dao.UserDAO'类型的限定bean可用:预计至少有1个bean可以作为autowire候选者。对于以下代码,我的基类是“com.springmvc”

我的控制器类是

package com.springmvc.controller;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.springmvc.dao.UserDAO;
import com.springmvc.dao.UserDAOImpl;
import com.springmvc.type.User;


@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private UserDAO usrdao;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {

        logger.info("Welcome home! The client locale is {}.", locale);

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);       
        String formattedDate = dateFormat.format(date);     
        model.addAttribute("Date", formattedDate );     
        return "index";
    }
    /*
    @RequestMapping(value="/login",method = RequestMethod.POST)
    public String welcome(Locale locale,Model model,@ModelAttribute("LoginBean")LoginBean login){

        if(login != null && login.getUserName() != "" && login.getPassword() != ""){

            if(login.getUserName().equals("ajith") && login.getPassword().equals("123")){

                model.addAttribute("msg","Welcome "+login.getUserName());
                return "welcome";   
            }
            else{

                model.addAttribute("error","Invalid Login Details");
                return "index";
            }
        }
        else{

            model.addAttribute("error","Please Enter Login Details");
            return "index";
        }
    }
    */
    @RequestMapping(value="/checkLogin",method = RequestMethod.POST)
    public String CheckUser(Model model,@ModelAttribute("User")User user){

        if(user != null && user.getUserName() != "" && user.getPass() != ""){

            boolean isActive            = usrdao.isActiveUser(user.getUserName(), user.getPass());

            if(isActive){
                List<User> usrLst   = usrdao.ListAllUsers();
                model.addAttribute("message",user.getUserName());
                model.addAttribute("usrLst",usrLst);
                return "home";
            }
            else{
                model.addAttribute("error","Invalid Login Details");
                return "index";
            }               
        }
        return "index";     
    }   
    @RequestMapping(value="/createUser", method = RequestMethod.GET)
    public String RedirectCreateUser(Model model){

        return "createUser";
    }

    public String createUser(Model model, @ModelAttribute User user){       


        usrdao.CreateOrUpdateUser(user);        
        return "";
    }
}

UserDAO的

package com.springmvc.dao;

import java.util.List;

import com.springmvc.type.User;

public interface UserDAO {

    public void CreateOrUpdateUser(User user);
    public void DeleteUser(int uid);
    public User GetUser(int uid);
    public List<User> ListAllUsers();
    public boolean isActiveUser(String userName,String pass);
}

在UserDAOImpl

package com.springmvc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Repository;

import com.springmvc.type.User;

@Repository("UserDAO")
public class UserDAOImpl implements UserDAO {

    private JdbcTemplate jdbc;  
    /*
    public void setJdbc(JdbcTemplate jdbc) {
        this.jdbc = jdbc;
    }
    */
    public UserDAOImpl(DataSource DS) {
        jdbc = new JdbcTemplate(DS);
    }

    @Override
    public void CreateOrUpdateUser(User user) {

        if(user.getUid() == 0){
            String query    = "INSERT INTO userdetails(UserName,Name,Pass) VALUES(?,?,?)";
            jdbc.update(query,user.getUserName(),user.getName(),user.getPass());
        }
        else{
            String query    = "UPDATE userdetails SET UserName = ?,Name = ?,Pass = ?,isActive = ? WHERE id = ?";
            jdbc.update(query, user.getUserName(),user.getName(),user.getPass(),user.getIsActive(),user.getUid());
        }
    }

    @Override
    public void DeleteUser(int uid) {

        String query    = "DELETE FROM userdetails WHERE id = ?";
        jdbc.update(query, uid);
    }

    @Override
    public User GetUser(int uid) {

        String query    = "SELECT * FROM userdetails WHERE id = ?";
        return  jdbc.query(query, new ResultSetExtractor<User>() {

            @Override
            public User extractData(ResultSet rs) throws SQLException, DataAccessException {

                if(rs.next()){

                    User user = new User();
                    user.setUid(rs.getInt("id"));
                    user.setUserName(rs.getString("UserName"));
                    user.setName(rs.getString("Name"));
                    user.setIsActive(rs.getInt("isActive"));
                    return user;
                }
                return null;
            }
        });     
    }

    @Override
    public List<User> ListAllUsers() {

        String query        = "SELECT * FROM userdetails";
        List <User> usrLst  = jdbc.query(query, new RowMapper<User>() {

            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {

                User user   = new User();
                user.setUid(rs.getInt("id"));
                user.setUserName(rs.getString("UserName"));
                user.setName(rs.getString("Name"));
                user.setIsActive(rs.getInt("isActive"));
                return user;
            }
        });
        return usrLst;
    }

    @Override
    public boolean isActiveUser(String userName, String pass) {

        String query    = "SELECT id FROM userdetails WHERE UserName = ? AND Pass = ? AND isActive = 1";    
        SqlRowSet rs    = jdbc.queryForRowSet(query,userName,pass); 
        if(rs.next())
            return true;
        else
            return false;
    }

}

谢谢

2 个答案:

答案 0 :(得分:0)

UserDAOImpl没有默认构造函数。要么添加默认构造函数,要么自动装配构造arg。

答案 1 :(得分:0)

Spring正在使用强大的命名约定。 尝试将变量userDao用作自动装配变量,并在@repository中使用相同的字符串。

在这里看到这个小解释:

https://www.tutorialspoint.com/spring/spring_beans_autowiring.htm