springboot无法自动装配的问题怎么解决

发布时间:2022-01-19 13:26:30 作者:kk
来源:亿速云 阅读:1746

本篇文章为大家展示了springboot无法自动装配的问题怎么解决,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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;
    }

这样他就不会报错了。

springboot是什么

springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。

上述内容就是springboot无法自动装配的问题怎么解决,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Springboot如何实现自动装配
  2. SpringBoot如何启动及自动装配

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot

上一篇:OpenCV是怎么实现简单套索工具

下一篇:html5中有哪些常用框架

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》