SpringBoot 自定义controller无法扫描到的问题

SpringBoot 自定义controller路由找不到,原因是启动类和自定义的Controller包不在同一级目录下。

官方建议application.java放的位置: 其余Bean与application.java放在同一个包路径下,但是不建议这么做。


解决的办法: 
1、把自建的controller类放到启动类同一级目录下(不建议这样做)。

2、把启动类@RestController @EnableAutoConfiguration注解改成@SpringBootApplication。

3、启动类@RestController @EnableAutoConfiguration注解再加上@ComponentScan注解。

使用springboot扫描的两种注解配置方式: 
1、@Controller 
   @EnableAutoConfiguration 
   @ComponentScan

2、@SpringBootApplication

      @ComponentScan

总结:@SpringBootApplication注解等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan,但是在实际运用时好像其他包中的Bean没有扫描发现,再次使用@ComponentScan指定包名称后发现了其他Bean,后续若有其他方法,会再次修改声明


猜你喜欢

转载自blog.csdn.net/m0_37135421/article/details/80880917
今日推荐