Springboot启动application报错:Field userMapper in xxx required a bean of type xx that could not be found

本文详细解析了SpringBoot结合MyBatis时遇到的典型配置错误,包括Mapper扫描缺失、依赖注入失败等问题,并提供了具体的解决方案,如正确使用@MapperScan注解,确保DAO接口被正确扫描和注册。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2018-07-24 15:38:07.647  INFO 20368 --- [           main] c.e.playspring.PlayspringApplication     : Starting PlayspringApplication on LAPTOP-P96OGO0J with PID 20368 (C:\project\playspring\target\classes started by DK in C:\project\playspring)
2018-07-24 15:38:07.651  INFO 20368 --- [           main] c.e.playspring.PlayspringApplication     : No active profile set, falling back to default profiles: default
2018-07-24 15:38:07.729  INFO 20368 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@13e39c73: startup date [Tue Jul 24 15:38:07 CST 2018]; root of context hierarchy
2018-07-24 15:38:08.376  WARN 20368 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.sam.project.*.mapper]' package. Please check your configuration.
2018-07-24 15:38:08.518  WARN 20368 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.example.playspring]' package. Please check your configuration.
2018-07-24 15:38:09.307  INFO 20368 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8088 (http)
2018-07-24 15:38:09.344  INFO 20368 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-07-24 15:38:09.345  INFO 20368 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-07-24 15:38:09.350  INFO 20368 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_171\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Java\jdk1.8.0_171\bin;C:\Program Files\Java\jdk1.8.0_171\jre\bin;C:\Program Files\Git\cmd;C:\Users\DK\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Bandizip\;.]
2018-07-24 15:38:09.533  INFO 20368 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-07-24 15:38:09.533  INFO 20368 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1806 ms
2018-07-24 15:38:09.816  INFO 20368 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-07-24 15:38:09.817  INFO 20368 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-07-24 15:38:09.818  INFO 20368 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-07-24 15:38:09.818  INFO 20368 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-07-24 15:38:09.819  INFO 20368 --- [ost-startStop-1] .s.DelegatingFilterProxyRegistrationBean : Mapping filter: 'springSecurityFilterChain' to: [/*]
2018-07-24 15:38:09.820  INFO 20368 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-07-24 15:38:09.874  WARN 20368 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.playspring.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2018-07-24 15:38:09.876  INFO 20368 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-07-24 15:38:09.909  INFO 20368 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-07-24 15:38:10.027 ERROR 20368 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userMapper in com.example.playspring.service.UserService required a bean of type 'com.example.playspring.mapper.UserMapper' that could not be found.


Action:

Consider defining a bean of type 'com.example.playspring.mapper.UserMapper' in your configuration.

因为刚开始玩spring boot ,也具有很多独特的注解,遇到了一些问题。目前demo跑起来了~

spring boot + mybatis 配置参考:https://blog.youkuaiyun.com/vtopqx/article/details/77719130#commentsedit

不过代码中启动类中缺少内容

解决办法:

在主函数类上添加以上注解,可以扫描dao包中的所有接口,替代在每个dao中写@Mapper注解,这样会提高耦合度。

加上@MapperScan注解 后面是mapper接口的类的地址

@MapperScan("com.example.playspring.mapper")

@MapperScan的理解:使用该注解,可以免去每个Mapper类都加上@Mapper

这个注解也可以同时扫描多个包,

如果如果mapper类没有在Spring Boot主程序可以扫描的包或者子包下面,可以使用如下方式进行配置

 

  1. @SpringBootApplication  
  2. @MapperScan({"com.example.*.mapper","org.example.*.mapper"})  
  3. public class App {  
  4.     public static void main(String[] args) {  
  5.        SpringApplication.run(App.class, args);  
  6.     }  

 

在Spring Boot应用中,当出现 `Field XXX required a bean of type 'XXX' that could not be found` 的错误时,表示Spring容器无法找到指定类型的Bean以完成依赖注入。以下是几种常见原因及解决方案: ### 1. 没有正确使用注解标记组件 确保相关类或接口已经通过适当的注解注册为Spring Bean。例如: - **Mapper接口** 应该添加 `@Mapper` 注解,或者在主类上使用 `@MapperScan` 来启用扫描。 - **DAO/Repository接口** 应添加 `@Repository` 注解。 - **Service类** 应添加 `@Service` 注解。 - **Controller类** 应添加 `@Controller` 或 `@RestController` 注解。 ```java @Repository public interface UserTextMapper { // 方法定义 } ``` 如果未正确标注,Spring将不会将其纳入IoC容器管理[^3]。 --- ### 2. 包扫描路径配置不完整 Spring Boot默认只会扫描主类所在包及其子包下的组件。如果目标Bean位于其他包路径下,则需要显式配置扫描路径: ```java @SpringBootApplication(scanBasePackages = {"com.hbx.xxx", "com.springboot.demo.dao"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 也可以单独使用 `@ComponentScan` 来指定更多扫描范围: ```java @ComponentScan({"com.hbx.xxx", "com.springboot.demo.dao"}) @Configuration public class AppConfig { } ``` 否则,即使正确标注了注解,Spring也无法发现并创建这些Bean[^2]。 --- ### 3. 使用Feign Client时缺少@EnableFeignClients 在整合Spring Cloud Feign时,若提示找不到Feign客户端的Bean(如 `PurchaseServiceImpl` 中的 `ProductFeignService`),请确认是否在启动类或配置类中添加了 `@EnableFeignClients` 注解,并指定了正确的包路径: ```java @EnableFeignClients(basePackages = "com.atguigu.gulimall.ware.feign") @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 否则Feign客户端将不会被生成和注册到Spring上下文中[^4]。 --- ### 4. 循环依赖导致Bean创建失败 有时虽然所有注解都正确,但仍然报错,可能是因为存在循环依赖问题。可以通过添加 `@Lazy` 注解来延迟加载某个Bean,从而打破循环依赖链: ```java @Autowired @Lazy private ProductFeignService productFeignService; ``` 此外,某些情况下Bean依赖的配置文件尚未加载完成(如使用 `@Profile` 加载特定环境配置),也会导致Bean初始化失败,此时同样可以尝试使用懒加载方式解决[^5]。 --- ### 5. 构造器或Setter注入替代字段注入 如果使用字段注入(`@Autowired` 直接标注在字段上)遇到问题,可尝试改为构造器注入或Setter方法注入,提高可测试性和清晰度: #### 构造器注入示例: ```java @Service public class UserService { private final UserRepository userRepository; @Autowired public UserService(UserRepository userRepository) { this.userRepository = userRepository; } } ``` #### Setter注入示例: ```java @Service public class UserService { private UserRepository userRepository; @Autowired public void setUserRepository(UserRepository userRepository) { this.userRepository = userRepository; } } ``` 这种方式有助于避免某些注入失败的情况,尤其是在单元测试或复杂依赖场景中[^3]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值