• springboot的Web项目编译运行时提示错误:Field userService in com.cetc.UserManger.controller.UserController required a bean of type 'com.cetc.UserManger.service.UserService' that could not be found.


    错误描述:

      springboot的Web项目编译运行时提示错误:Field userService in com.cetc.UserManger.controller.UserController required a bean of type 'com.cetc.UserManger.service.UserService' that could not be found.

    解决办法:

      在提示错误的service的实现类impl上添加注解@Service。如下代码所示:

    UserRepository的代码如下:
    public interface UserRepository extends JpaRepository<User,Integer> {
    }
    UserService的代码如下:
    public interface UserService {
        public Page<User> findAll(Pageable pageable);
    }
    UserServiceImpl的代码如下:
    @Service
    public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public Page<User> findAll(Pageable pageable){
    return userRepository.findAll(pageable);
    }
    }
    UserController的代码如下:
    @RestController
    @RequestMapping("/user")
    public class UserController {
        @Autowired
        private UserService userService;
    
        @GetMapping("/findAll/{page}/{size}")
        public Page<User> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
            PageRequest request=PageRequest.of(page,size);//注意:page的索引是从0开始的,size的索引是从1开始的
            return userService.findAll(request);
        }
    }
  • 相关阅读:
    二叉搜索查找排序树
    多项式运算
    赫夫曼编码及应用
    利用python画出动态高优先权优先调度
    利用python画出SJF调度图
    支持向量机
    fisher线性分类器
    Codeforces Round #520 (Div. 2)
    Codeforces Round #510 (Div. 2)
    Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/14672193.html
Copyright © 2020-2023  润新知