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 jackson提供对LocalDate的支持方式
这篇文章主要介绍了SpringBoot jackson提供对LocalDate的支持方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-01-01
Spring Boot中MongoTemplate从入门到实战深度解析
本文介绍了Spring Data MongoDB中的MongoTemplate,包括主要特性、核心配置、基础CRUD、高级查询与聚合操作、GridFS操作、性能优化与监控以及最佳实践,通过全面的示例项目,展示了如何在Spring Boot应用中使用MongoTemplate进行数据库操作,感兴趣的朋友跟随小编一起看看吧2026-01-01
基于Java实现ssh命令登录主机执行shell命令过程解析
这篇文章主要介绍了基于Java实现ssh命令登录主机执行shell命令过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-12-12


最新评论