开发者

解决bean对象注入报错:Field in required a bean of type‘‘that could not be found.问题

目录
  • 问题现象
  • 问题分析
    • 前提
    • 建议
  • 解决方法
    • 总结

      问题现象

      今天在运行新项目的时候,出现了一下报错:

      Field XxxUserExtDao in com.xxx.auth.service.controller.abs.BaseController required a bean of type 'com.xxx.auth.service.mapper.run.XxxUserExtDao' that androidcould not be found.

      解决bean对象注入报错:Field in required a bean of type‘‘that could not be found.问题

      问题分析

      前提

      在完成上述操作之前,请先基于myBATis创建好dao层接口类对应的dao层实现类(一般都是以XML文件的形式,并确保mapper标签的namespace属性指定为dao层接口类的全路径。

      从报错信息可知:

      这是因为spring的Ioc容器在注入bean对象的时候,在 BaseController 类中检测到了 @Autowire编程客栈d 注解,于是就会去寻找这个 XxxUserExtDao 对象的注入方式:

      解决bean对象注入报错:Field in required a bean of type‘‘that could not be found.问题

      解决bean对象注入报错:Field in required a bean of type‘‘that could not be found.问题

      然后检测到 XxxUserExtDao 这个类中带有 @Component 注解,该注解用于除了@Controller(控制层)@Service(业务层)@Repository(dao层的实现类)之外,需要注入成bean对象的类;

      注意:

      这里值得一提的是:由于dao层的实现类对于项目结构而言,太冗余了,每写一个dao层接口,就得手写一个实现类,于是我们可以通过注解的方式,来实现自动创建dao层实现类,而不再需要自己手动创建实现类和添加@Repository注解了,方式有两种:

      1. 在 dao层接口类 上添加 @Mahttp://www.devze.compper注解。 作用:在接口类上添加了 @Mapper注解 ,在编译之后会自动生成相应的接口实现类:

      解决bean对象注入报错:Field in required a bean of type‘‘that could not be found.问题

      2. 在 主类(启动类)上添加 @MapperScahttp://www.devze.comn注解,并指定dao层接口的包路径,在编译之后会自动生成相应的接口实现类

      解决bean对象注入报错:Field in required a bean of type‘‘that could not be found.问题

      建议

      两种方法取其中一种即可,这里建议使用方法2,这样就不需要每个dao层都添加@Mapper注解了!!!

      解决方法

      主类(启动类)上添加 @MapperScan注解,并指定dao层接口的包路径,在编译之后会自动生成相应的接口实现类

      解决bean对象注入报错:Field in required a bean of type‘‘that could not be found.问题

      启动成功:

      解决bean对象注入报错:Field in required a bean of type‘‘that could not be found.问题

      调用接口成功:

      解决bean对象注入报错:Field in required a bean of type‘‘that could not be found.问题

      总结

      以上为个www.devze.com人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

      0

      上一篇:

      下一篇:

      精彩评论

      暂无评论...
      验证码 换一张
      取 消

      最新开发

      开发排行榜