欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

springboot 无法自动装配的问题

时间:2022-02-19 09:39:48|栏目:JAVA代码|点击:

springboot 无法自动装配

@Autowired 报错:无法自动装配

基本上是因为

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

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

放到org.example下,问题解决

原因

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

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

这样他就不会报错了。

上一篇:Spring-AOP 静态普通方法名匹配切面操作

栏    目:JAVA代码

下一篇:MyBatis查询无记录时的返回值问题

本文标题:springboot 无法自动装配的问题

本文地址:http://www.codeinn.net/misctech/193918.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有