在開發過程中,我們常常會遇到各種錯誤和異常。其中一個常見的問題是在使用Spring框架的時候,遇到了類似於"com.example.demo.service.UserServiceImpl 中建構函數的參數0 需要類型為“com.example.demo.dao.UserDao”的bean"的錯誤訊息。這個錯誤提示意味著在UserServiceImpl類別的建構子中,第一個參數需要注入一個型別為UserDao的bean,但是系統找不到對應的bean。解決這個問題的方法有很多,本文將為大家介紹一個簡單有效的解決方案。
誰能幫我除錯這個錯誤
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.
以下是我的文件:
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); } }
我在 demoapplication.java 中嘗試過 @componentscan("com.example.demo.service")
但它不起作用。
我還嘗試放置@autowire並用@service標記服務。我還檢查了所有其他註釋,沒有發現任何其他遺漏的內容
我希望有一個乾淨的構建和對 api 的訪問
錯誤
您缺少 userservice
的實作。如果您想要保留 @repository
(userdao
) 的目前實現,那麼您可以如下重寫您的服務:
@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); }
這應該使其可供 usercontroller
使用。
以上是com.example.demo.service.UserServiceImpl 中建構函數的參數 0 需要類型為「com.example.demo.dao.UserDao」的 bean的詳細內容。更多資訊請關注PHP中文網其他相關文章!