Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)

在项目启动中出现这个报错,让我感到困惑,经过我一番捣腾终于知道是啥原因,下面解释一下为什么会出现这样的错误:

<span style="font-size:12px;">org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderInfoUCMPController':
 Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
 Could not autowire field: com.sf.egmas.dcn.web.service.orderInfo.OrderInfoUCMPService com.sf.egmas.dcn.web.controller.orderInfo.OrderInfoUCMPController
.orderInfoUCMPService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'OrderInfoUCMPService': 
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
 Could not autowire field: com.sf.egmas.dcn.web.utils.UcmpUtil com.sf.egmas.dcn.web.service.orderInfo.impl.OrderInfoUCMPServiceImpl.ucmpUtil;
 nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [com.sf.egmas.dcn.web.utils.UcmpUtil] found for dependency: expected at least 1 bean which qualifies as autowire candidate 
for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}</span>

Autowired 根据类型去spring容器找,找不到那个类,就会报错,这是引起这个错误的原因;

下面普及一下这个注解:

@Autowired(required=false)当根据类型去找,找不到时,注入一个空。
@Autowired(required=true)当根据类型去找,找不到时,抛出异常信息

@component(把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)

这里有两种解决方案:

第一种:使用@Autowired(required=false),但是这样会有一个问题,虽然不会报错了,但是在使用的时候调用不到里面的方法(不建议);

第二种:使用@Component泛指组件,当组件不好标记的时候,可以用这个注解来标注;

如果不使用@component 来标注,你使用注入的时候,肯定在spring容器中找不到相应的bean;

示例:

<span style="font-size:14px;">@Component
public class UcmpUtil {}</span>


我今天在网上看了下,报这种错误的人挺多的,但是没有人说使用这个注解来标注;

我希望会有更多人看到我这篇文章,嘿嘿。。







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

雨润泽林

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

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

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

打赏作者

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

抵扣说明:

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

余额充值