【java】Could not autowire. No beans of ‘XXX‘ type found.

@MapKey is required 提示异常

在这里插入图片描述
在报错的方法上添加@SuppressWarnings("MybatisXMapperMethodInspection")可关闭报错。

@Autowired 出现:Could not autowire. No beans of ‘XXX’ type found.

出现:Could not autowire. No beans of ‘XXX’ type found.
无法自动连线。找不到“XXX”类型的bean。

在这里插入图片描述
如果实在在控制层调用 @Autowired(required = false)忽略错误
@Autowired:表示自动注入,自动从spring的上下文找到合适的bean来注入

@Resource:表示按指定名称注入,@Resource 可以通过 byName 和 byType的方式注入, 默认先按 byName的方式进行匹配,如果匹配不到,再按 byType的方式进行匹配。 当然,还可以为 @Service和@Resource 添加 name 这个属性来区分不同的实现。

@Component:表示一种泛指,被标记类即组件,Spring扫描注解配置时,会标记这些类要生成对应的bean。

@Qualifier@Autowired配合使用,当一个接口有多个实现的时候,@Qualifier的value定了具体调用哪个类的实现(需要在实现类中通过@Service来表示每个不同的bean),也就是说指定了注入bean的名称。

@Autowired@Resource是用来修饰字段,构造函数,或者方法,其作用是注入bean。
@Service@Controller@Repository@Component则是用来修饰类,标记这些类要生成bean。

Controller、Service、Repository分别作用类是控制层类、业务层类、数据访问层类,Spring扫描注解配置时,会标记这些类要生成对应的bean。

@Autowired(required = false) Field injection is not recommended

Field injection is not recommended 不建议现场注入

在这里插入图片描述
这个又是一个新的描述?TODO

参考

[Java Spring FAQs] Field injection is not recommended and Injection guidelines in Java Spring

Field injection is not recommended – Spring IOC

Could not autowire.No beans of ‘XXXX‘type found的解决方法(详细解析其用法注解)

@MapKey is required 提示异常

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

掘金者说

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

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

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

打赏作者

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

抵扣说明:

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

余额充值