@Resource注入mapper失败的原因

业务需要,单独创建一个服务用来使用MyBatis-Generator自动生成entity和注解形式的mapper,做最基础的crud, 其他的复杂的业务(比如我们一些业务是有某些字段条件下的,或者是多条件查询的),使用更加灵活的xml动态sql来实现。然后在服务里再封装一层dao,集成注解mapper和xml的mapper接口。

因为有2个mapper接口,要在启动类上添加mapper扫包接口 @MapperScan({"接口路径", “接口路径”})
封装的dao使用Repository 声明接口, @Resource注入不同的mapper。。
在启动的时候出现了一个异常:

The bean ‘xxxService‘ could not be injected as a ‘AaaXxxService‘ because it is a JDK dynamic proxy that implements: 

Action:  
 
Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.

提示mapper是jdk的代理类,不能以mapper的形式注入。。
网上查询答案,很多都是说是事务的问题,只要添加注解@EnableTransactionManagement(proxyTargetClass = true)来解决,
但是我代码里并没有使用到事务。。
后来看到这个答案 异常原因排查 说是因为名字不规范导致注入失败,查询@Autowired@Resource注解的区别 @Autowired和@Resource注解的区别。
原来是 @Resource默认是以bean的name为注入策略的。bean的名字最好与bean的名字保持一致。
最后把所有bean的name都改成bean的小驼峰,异常解决。。

你可能感兴趣的:(遇到的问题)