zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    大二上每日总结
    大二上每日总结
    大二上每日总结
    大二上每日总结
    大二上每日总结
    大二上每日总结
    MyBatis(八)MyBatis逆向工程
    MyBatis(七)SSM 整合:MyBatisSpringSpringMVC 整合
    MyBatis(九)工作原理 之 框架分层架构
    Interesting Finds: 2008.06.01
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/14672193.html
Copyright © 2011-2022 走看看