admin管理员组

文章数量:1794759

解决错误:Consider defining a bean of type ‘xxxrService‘ in your configuration

解决错误:Consider defining a bean of type ‘xxxrService‘ in your configuration

文章目录
      • 一、问题描述
      • 二、解决方法

一、问题描述

    运行 SpringBoot 启动类,报错:     可以看到,它是说 WeiXinPayController 中,用到了 OrderService ,但是呢,Spring 扫描不到。

二、解决方法

    其实可以看出来, WeiXinPay 和 Order 分属两个工程,具有各自的功能,在一个工程里调用另一个工程的 service 层,其实是不妥的,如果业务有交叉,可以用 feign 的方式调用 controller 层。     事已至此,先解决一下这个错误吧。     看看 SpringBootApplication 源码:

@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootconfiguration @EnableAutoConfiguration @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication {

    可以看到,@ComponentScan ,只能扫描与控制器在同一个包下以及其子包下的 @Component 注解,以及能将指定注解的类自动注册为 bean 的@Service 、@Controller 和 @ Repository,所以, WeiXinPayController 想识别到 OrderService,需要在 pay 的启动类上使用注解 :

@ComponentScan(basePackages = {"com.changgou.order.service"})

本文标签: 错误definingbeanconfigurationxxxrService