`
arpenker
  • 浏览: 330798 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

IDEA报Could not autowire. No beans of 'ProductCategoryMapper' type foundu并标红浪

阅读更多

IDEA报Could not autowire. No beans of 'ProductCategoryMapper' type found(详细分析).

 

今天在写springboot案例的时候发现了Could not autowire. No beans of 'ProductCategoryMapper' type found.

 

意思为@Autowired没有起效,不能自动注入,beans对象的类型没有找到

 

 

 

上网查询资料发现可能错误如下:

 

1、@Service注解的包倒错了

正确的包为:

 

import org.springframework.stereotype.Service;

2、在你的Application类中 加上注解 @MapperScan

 

 

3、Mapper层(即dao接口层)没有加注解@Repository

我的错误为第三种,加上后不再报错

 

原因分析如下:

 

@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作,我们也要清楚,@Autowired是根据类型进行自动装配的。

 

@Repository(标识持久层)注解修饰哪个类,则表明这个类具有对对象进行CRUD(增删改查)的功能,而且@Repository是@Component注解的一个派生品,所以被@Repository注解的类可以自动的被@ComponentScan 通过路径扫描给找到。(这也在一定程度上解释了,为什么被@Repository注解的类才能@Autowired)

 

现象为报引用错。可是明明有这个类,就是引用不到呢。

最终,使用@Repository标记了mapper类。问题解决。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics