spring里面有bean,但是@Autowrite注入不进去

本文介绍了一个具体的Spring MVC应用案例,详细展示了Controller层如何通过注解处理视频相关的请求,并解释了Service层实现的具体业务逻辑。同时,文章还提到了由于方法权限修饰符设置不当导致的问题及解决方法。
摘要由CSDN通过智能技术生成

caontroller层

/**
 * @author xu
 * @date:2022/3/2718:27
 */
@RequestMapping("/video")
@RestController
public class VedioController {
//    @Resource(name= "videoServiceImpl")
    @Autowired
    private VideoService videoService;

    @RequestMapping("/list")
    @LogAnnotation(module = "视频",operation = "获取视频列表")
    private Result getVedioList(@RequestBody QueryParams queryParams){
        DataVo vedioList = videoService.getVedioList(queryParams);
        return Result.success(vedioList);
    }

    @RequestMapping("/add")
    @LogAnnotation(module = "视频",operation = "获取视频列表")
    private Result addVeedio(@RequestBody VideoParams videoParams){
        Result result = videoService.addVedio(videoParams);
        return Result.success(result);
    }
    @RequestMapping("/del")
    @LogAnnotation(module = "视频",operation = "获取视频列表")
    private Result delVedio(@RequestParam Integer id){
        videoService.delVedio(id);
        return Result.success(null);
    }
    @RequestMapping("/edit")
    @LogAnnotation(module = "视频",operation = "获取视频列表")
    private Result editVedio(@RequestBody VideoParams videoParams){
        return Result.success(videoService.editVedio(videoParams));
    }

}

service层

@Service
@Transactional
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private DataVo dataVo;

    @Override
    public DataVo getVedioList(QueryParams queryParams) {
        Page<Video> page = new Page<>(queryParams.getCurrentPage(), queryParams.getPageSize());
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        Page<Video> vedioPage = videoMapper.selectPage(page,queryWrapper);
        List<VideoParams> videoParamsList = copyVedioList(vedioPage.getRecords());
//        DataVo dataVo = new DataVo(videoParamsList, Math.toIntExact(vedioPage.getTotal()));
        dataVo.setList(videoParamsList);
        dataVo.setTotal(Math.toIntExact(vedioPage.getTotal()));
        return dataVo;
    }

    private List<VideoParams> copyVedioList(List<Video> videoList){
        List<VideoParams> videoParamsList = new ArrayList<>();
        for (Video video : videoList) {
            videoParamsList.add(copyVedio(video));
        }
        return videoParamsList;
    }

    private VideoParams copyVedio(Video video){
        VideoParams videoParams = new VideoParams();
        BeanUtils.copyProperties(video, videoParams);
        Teacher teacher= teacherService.findTeacherById(video.getUserId());
        videoParams.setCreateName(teacher.getName());
        return videoParams;
    }

    @Override
    public Result editVedio(VideoParams videoParams) {
//        User user = UserThreadLocal.get();
//        vedioParams.setUserId(user.getId());
        Video video = new Video();
        BeanUtils.copyProperties(videoParams, video);
        videoMapper.updateById(video);
        return Result.success(null);
    }

    @Override
    public Result addVedio(VideoParams videoParams) {
//        User user = UserThreadLocal.get();
//        vedioParams.setUserId(user.getId());
        Video video = new Video();
        BeanUtils.copyProperties(videoParams, video);
        videoMapper.insert(video);
        return Result.success(null);
    }

    @Override
    public void delVedio(Integer id) {
        videoMapper.deleteById(id);
    }
}

这个是打印spring里面的bean名称,红框中的bean就是注入不进去的bean
在这里插入图片描述
最后问题发现是因为controller中的方法都是private,然后就改成public就没有问题了
最后把打印bean的代码
在这里插入图片描述

 @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }

        };
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值