springboot 无法自动装配的问题
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;
}这样他就不会报错了。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot整合ip2region实现使用ip监控用户访问城市的详细过程
这篇文章主要介绍了SpringBoot整合ip2region实现使用ip监控用户访问城市,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-07-07
深入理解Java8新特性之Stream API的创建方式和中间操作步骤
Stream是Java8的一大亮点,是对容器对象功能的增强,它专注于对容器对象进行各种非常便利、高效的 聚合操作(aggregate operation)或者大批量数据操作。Stream API借助于同样新出现的Lambda表达式,极大的提高编程效率和程序可读性,感兴趣的朋友快来看看吧2021-11-11
spring-boot集成spring-security的oauth2实现github登录网站的示例
本篇文章主要介绍了spring-boot集成spring-security的oauth2实现github登录网站的示例,非常具有实用价值,需要的朋友可以参考下2017-10-10
java.io.UncheckedIOException: Cannot delete C
本文主要介绍了java.io.UncheckedIOException: Cannot delete C:\Users\guo\AppData\Local\Temp\tomcat.8081问题,具有一定的参考价值,感兴趣的可以了解一下2024-05-05


最新评论