search
HomeJavaParameter 0 of constructor in com.example.demo.service.UserServiceImpl requires a bean of type 'com.example.demo.dao.UserDao'

During the development process, we often encounter various errors and exceptions. One of the common problems is that when using the Spring framework, you encounter something similar to "Parameter 0 of the constructor in com.example.demo.service.UserServiceImpl requires a bean of type "com.example.demo.dao.UserDao"" error message. This error message means that in the constructor of the UserServiceImpl class, the first parameter needs to be injected with a bean of type UserDao, but the system cannot find the corresponding bean. There are many ways to solve this problem, and this article will introduce you to a simple and effective solution.

Question content

Who can help me debug this error

parameter 0 of constructor in com.example.demo.service.userserviceimpl required a 
bean of type 'com.example.demo.dao.userdao' that could not be found.

action:

consider defining a bean of type 'com.example.demo.dao.userdao' in your configuration.

The following are my files:

usercontroller.java

package com.example.demo.controller;

import com.example.demo.model.user;
import com.example.demo.service.userservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.*;

import java.util.list;

@restcontroller
@requestmapping("/api/users")
public class usercontroller {

    @autowired
    private final userservice userservice;
    
    public usercontroller(userservice userservice) {
        this.userservice = userservice;
    }

    @getmapping("/{userid}")
    public user getuserbyid(@pathvariable long userid) {
        return userservice.getuserbyid(userid);
    }

    @getmapping
    public list<user> getallusers() {
        return userservice.getallusers();
    }

    @postmapping
    public long adduser(@requestbody user user) {
        return userservice.adduser(user);
    }

    @putmapping("/{userid}")
    public void updateuser(@pathvariable long userid, @requestbody user user) {
        user.setuserid(userid);
        userservice.updateuser(user);
    }

    @deletemapping("/{userid}")
    public void deleteuser(@pathvariable long userid) {
        userservice.deleteuser(userid);
    }
}

userservice.java

package com.example.demo.service;

import com.example.demo.model.user;
import org.springframework.stereotype.component;
import org.springframework.stereotype.service;

import java.util.list;

public interface userservice {
    user getuserbyid(long userid);

    list<user> getallusers();

    long adduser(user user);

    void updateuser(user user);

    void deleteuser(long userid);
}

userserviceimpl.java

package com.example.demo.service;

import com.example.demo.dao.userdao;
import com.example.demo.model.user;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;

import java.util.list;

@service
public class userserviceimpl implements userservice {
    
    private final userdao userdao;

    @autowired
    public userserviceimpl(userdao userdao) {
        this.userdao = userdao;
    }

    @override
    public user getuserbyid(long userid) {
        return userdao.getuserbyid(userid);
    }

    @override
    public list<user> getallusers() {
        return userdao.getallusers();
    }

    @override
    public long adduser(user user) {
        return userdao.adduser(user);
    }

    @override
    public void updateuser(user user) {
        userdao.updateuser(user);
    }

    @override
    public void deleteuser(long userid) {
        userdao.deleteuser(userid);
    }
}

userdaoimpl.java

package com.example.demo.dao;

import com.example.demo.model.user;
import org.springframework.jdbc.core.beanpropertyrowmapper;
import org.springframework.jdbc.core.jdbctemplate;
import org.springframework.stereotype.repository;

import java.util.list;

@repository
public class userdaoimpl implements userdao {

    private final jdbctemplate jdbctemplate;

    public userdaoimpl(jdbctemplate jdbctemplate) {
        this.jdbctemplate = jdbctemplate;
    }

    @override
    public user getuserbyid(long userid) {
        string sql = "select * from user where user_id = ?";
        return jdbctemplate.queryforobject(sql, new object[]{userid}, new beanpropertyrowmapper<>(user.class));
    }

    @override
    public list<user> getallusers() {
        string sql = "select * from user";
        return jdbctemplate.query(sql, new beanpropertyrowmapper<>(user.class));
    }

    @override
    public long adduser(user user) {
        string sql = "insert into user (first_name, last_name, email, user_avatar_url, podcast_id) " +
                "values (?, ?, ?, ?, ?)";
        jdbctemplate.update(sql, user.getfirstname(), user.getlastname(), user.getemail(),
                user.getuseravatarurl(), user.getpodcastid());

        // retrieve the auto-generated user_id
        return jdbctemplate.queryforobject("select last_insert_id()", long.class);
    }

    @override
    public void updateuser(user user) {
        string sql = "update user set first_name = ?, last_name = ?, email = ?, " +
                "user_avatar_url = ?, podcast_id = ? where user_id = ?";
        jdbctemplate.update(sql, user.getfirstname(), user.getlastname(), user.getemail(),
                user.getuseravatarurl(), user.getpodcastid(), user.getuserid());
    }

    @override
    public void deleteuser(long userid) {
        string sql = "delete from user where user_id = ?";
        jdbctemplate.update(sql, userid);
    }
}

userdao.java

package com.example.demo.dao;
import com.example.demo.model.user;

import java.util.list;

public interface userdao {
    user getuserbyid(long userid);

    list<user> getallusers();

    long adduser(user user);

    void updateuser(user user);

    void deleteuser(long userid);
}

demoapplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
//@ComponentScan("com.example.demo.service")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

I tried @componentscan("com.example.demo.service") in demoapplication.java but it doesn't work.

I also tried putting @autowire and marking the service with @service. I also checked all other comments and didn't find anything else missing

I want a clean build and access to the api

Error

Solution

You are missing the implementation of userservice. If you want to keep the current implementation of @repository (userdao) then you can rewrite your service as follows:

@Service
public class UserService {

    private final UserDao userDao;

    @Autowired
    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    // implement using your DAO
    User getUserById(Long userId);
    List<User> getAllUsers();
    Long addUser(User user);
    void updateUser(User user);
    void deleteUser(Long userId);
}

This should make it available to usercontroller.

The above is the detailed content of Parameter 0 of constructor in com.example.demo.service.UserServiceImpl requires a bean of type 'com.example.demo.dao.UserDao'. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)