springboot 无法自动装配的问题

作者:时时刻刻看着自己的心 时间:2021-05-29 06:14:30 

springboot 无法自动装配

@Autowired 报错:无法自动装配

springboot 无法自动装配的问题

基本上是因为

1、项目里有类似mybatis @Mapper这种第三方映射类,需要用到springboot autoconfigration扫描解析。

2、@SpringBootApplication类,没有放到java根目录下

放到org.example下,问题解决

springboot 无法自动装配的问题

原因

因为springboot只扫描@SpringBootApplication类目录及子目录下的自动配置:

springboot 无法自动装配的问题

For example, it will be used when scanning for @Entity classes. It is generally recommended that you place @EnableAutoConfiguration (if you're not using @SpringBootApplication) in a root package so that all sub-packages and classes can be searched.

真想骂他一句,约定就约定吧,能聪明点吗

无法自动装配。未找到“xxxMapper”类型的bean

Could not autowire. No beans of ‘xxxMapper’ type found.

说明Spring框架没有识别到你的xxxMapper中的类

也就是说,xxxMapper的类没有被Spring框架给管理,如果你所需要的类需要给Spring给管理,那么你得在他上面加上@Repository注解,这样你在service层自动注入时他才不会报错。

如果你得类不需要管理或者继承或实现一些规则

并且程序没有产生一些错误,那么这些都是可以被允许的。

@Repository
public interface AdminMapper  {
    public void xxx(){}
}
public class AdminServiceImpl  {
    @Autowired
    private AdminMapper adminMapper;
    }

这样他就不会报错了。

来源:https://blog.csdn.net/china_xuelei/article/details/119184678

标签:springboot,自动装配,装配
0
投稿

猜你喜欢

  • Spring Boot与Spring MVC Spring对比及核心概念

    2023-09-27 05:14:36
  • Java创建和启动线程的两种方式实例分析

    2023-12-05 08:26:59
  • C#回收机制之资源回收托管

    2022-03-04 13:18:20
  • 基于java实现简单发红包功能

    2023-08-09 17:06:40
  • Spring如何基于注解配置使用ehcache

    2022-08-16 03:24:32
  • Java实战之酒店人事管理系统的实现

    2021-06-02 16:44:37
  • IDEA 2022 中的Lombok 使用基础教程

    2023-04-09 21:57:09
  • Java开发学习 Eclipse项目有红感叹号解决之道

    2022-10-22 15:29:27
  • Java Socket实现单线程通信的方法示例

    2022-04-22 15:43:02
  • Java利用位运算实现乘法运算详解

    2023-03-19 20:29:24
  • Java异常处理之try...catch...语句的使用进阶

    2022-03-16 03:06:09
  • SpringBoot实现接口等幂次校验的示例代码

    2022-01-21 10:49:00
  • MyEclipse去除网上复制下来的代码带有的行号(正则去除行号)

    2023-09-15 21:59:23
  • Spring MVC中自定义拦截器的实例讲解

    2023-12-19 05:09:04
  • SpringBoot配置GlobalExceptionHandler全局异常处理器案例

    2023-06-11 12:14:36
  • java语言实现猜数字游戏

    2023-11-24 00:19:38
  • 详解JAVA类加载机制(推荐)

    2021-08-10 04:43:10
  • SpringBoot在一定时间内限制接口请求次数的实现示例

    2021-10-12 04:28:52
  • Java KindEditor粘贴图片自动上传到服务器功能实现

    2023-08-07 01:42:33
  • SpringBoot AOP使用笔记

    2023-12-09 13:25:50
  • asp之家 软件编程 m.aspxhome.com