SpringBoot(cloud)自动装配bean找不到类型的问题

网友投稿 3654 2022-10-31

SpringBoot(cloud)自动装配bean找不到类型的问题

SpringBoot(cloud)自动装配bean找不到类型的问题

目录SpringBoot自动装配bean找不到类型今天我就犯了因为boot扫不到包的问题看项目结构很明显无法自动装配。未找到“xxxMapper”类型的bean

SpringBoot自动装配bean找不到类型

Spring基于注解的@Autowired是比较常用的自动装配注解,但是会因为个人的疏忽,SSM进行配置的时候没有将对应bean所在包给扫描进去;或者使用Boot的时候,没有放在启动类所在包及其子包下导致报错。

今天我就犯了因为boot扫不到包的问题

Description:

Field empApi in feign_consumer.demo.controller.testController required a bean of type 'api.eApi' that could not be found.

The injection point has the following annotations:    - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'api.eApi' in your confhttp://iguration.

Process finished with exit code 1

看报错,很明显就是找不到Bean;换而言之就是bean对象没有装配到Spring容器中,导致启动的时候要装配缺找到。

看项目结构

很明显

在boot启动的时候扫描的是feign_consumer.demo这个包及其子包,而需要装配的bean就只在api这包下,Spring在启动的时候根本不会扫到api这个包(除非使用了@ComponentScan修改了约定)。所以就会导致项目启动报错。

这也提醒了我们,使用模块化开发要注意项目的完整性以及其工程结构;另外,代码规范也很重要。

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

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

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

如果的你得类不需要管理或者继承或实现一些规则,并且程序没有产生一些错误,那么这些都是可以被允许的。

@Repository

puhttp://blic interface AdminMapper {

public void xxx(){}

}

public class AdminServiceImpl {

@Autowired

private AdminMapper adminMapper;

}

这样他就不会报错了。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Usopp:一个app初始化框架,集成仅需一行代码
下一篇:LNKeyBoardManager 极其简单、友好的管理键盘遮盖的框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~