Spring Cloud框架@Autowired(required=true)报错问题

项目场景:

关于Spring Cloud微服务接入小框架记录


问题描述:

提示错误:@org.springframework.beans.factory.annotation.Autowired(required=true)

到百度或者其他博客上看都是关于@Autowired(required=true)的注入问题,这个问题也一度困扰了我很长时间

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-12-10 15:19:29 ERROR server-bu main org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field mealOrderStruct in com.yeal.service.impl.OrderServiceImpl required a bean of type 'com.yeal.struct.OrderStruct' 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 'com.yeal.struct.OrderStruct' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:50949', transport: 'socket'

Process finished with exit code 1

原因分析:

将这个该死的问题上百度一查会有很多博客告诉你设置成@Autowired(required=false)!诚不欺我,这个时候启动项目,不出意外还是一模一样的报错。如果你的不报错那么,你可以划走了·············


![错误位置](https://img-blog.csdnimg.cn/199016325f344cce93fb322d3dbfdff5.png) 可以看到正常注入的类都是有前面都是有一个绿色的箭头。很显然下面这个并没有被注入。 那么我第一个想到的是框架的问题,后来重新看了框架的使用,是没有问题的。

注入问题啊,注入到spring容器里面!知道问题所在就好解决问题了。
1.因为使用的mapstruct框架做DTO、entity、Vo之间的转换是用的
@Mapper(componentModel = "spring")
这个注解一直以为是用的@MapperScan(basePackages = {"com.yeal.**.mapper"})去扫描的试了半天,结果不是。
2.突然想起来@SpringBootApplication里面也是有扫描的注解也可以定义包

解决方案:

就在这个注解里面加上包的一个扫描就轻松搞定@SpringBootApplication(scanBasePackages = {"com.yeal"})

总结:

关于- @org.springframework.beans.factory.annotation.Autowired(required=true)报错,原因如下
1.可能是因为扫描注入问题:要么重复扫描,要么没有扫描到
2.@Autowired(required=false)

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望舒@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值