在做本地视频上传到服务器时在AutoWired自动过程中出现如下错误:
Description:
Field vodService in com.guli.vod.controller.UploadVodController required a bean of type 'com.guli.vod.service.VodService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.guli.vod.service.VodService' in your configuration.
原因:
bean没有注册到,所以VodService没有找到,所以无法自动注入。
解决思路:
可能是我VodService的实现类上没有加@Serivce注解
但是加上之后启动扫不到,这就怪了,我的代码感觉没问题。
controller:
service:
service实现类serviceImpl:
于是乎上网搜了搜解决办法,问题出现在application类的注SpringBootApplication上。
进入@SpringBootApplication的注解类中看看了上面的注解:
这个注解其实相当于上面这一堆注解的效果,其中一个注解就是@Component,在默认情况下只能扫描与控制器在同一个包下以及其子包下的@Component注解,以及能将指定注解的类自动注册为Bean的@Service@Controller和@ Repository,至此明白问题所在,之前我将接口与对应实现类放在了与控制器所在包的同一级目录下,这样的注解自然是无法被识别的。
问题分析出来的解决办法:
1 .将接口与对应的实现类放在与application启动类的同一个目录或者他的子目录下,这样注解可以被扫描到了。
2 .在指定的application类上加上这么一行注解,手动指定application类要扫描哪些包下的注解。
@SpringBootApplication
@ComponentScan("com.guli.vod.service.impl")
public class VodApplication {
public static void main(String[] args) {
SpringApplication.run(VodApplication.class);
}
}
参考博客:https://blog.csdn.net/a532672728/article/details/77702772