IDEA提示找不到Mapper接口:Could not autowire.No beans of 'xxxMapper' type found

前言

相信大多数互联网公司的持久层框架都是使用 Mybatis 框架,而大家在 Service 层引入自己编写的 Mapper 接口时应该会遇到下面的情况:
在这里插入图片描述
我们可以看到,上面的红色警告在提示我们,找不到 xxxMaper 这个类型的 bean。

为啥呢?

因为 @Mapper 这个注解是 Mybatis 提供的,而 @Autowried 注解是 Spring 提供的,IDEA能理解 Spring 的上下文,但是却和 Mybatis 关联不上。而且我们可以根据 @Autowried 源码看到,默认情况下,@Autowried 要求依赖对象必须存在,那么此时 IDEA 只能给个红色警告了。

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {

    /**
     * Declares whether the annotated dependency is required.
     * <p>Defaults to {@code true}.
     */
    boolean required() default true;

}

怎么解决?

虽然 IDEA 给了个红色警告,但是程序本身是没问题的,可以正常运行。可是代码里头有红色警告是相当的显眼的,不晓得情况的人还以为我么你的代码有问题呢?

下面我将会列出实践过的解决方案。
1、简单粗暴

直接关掉IDEA的警告提示,是不是很简单?是不是很粗暴?

2、麻烦粗暴,给@Autowried注解设置required=false
@Autowired(required = false)
private CarUserMapper carUserMapper;

这样就不会警告了。因为此时 @Autowried 不会再去校验 Mapper 接口是否为空。
缺点:每引入一个Mapper接口都需要设置上required=false,相当的麻烦,而且容易给别人造成误解:这个Mapper接口真的不需一定存在。

3、@Autowried替换为@Resource
@Resource
private CarUserMapper carUserMapper;

此时也不会给红色警告了。@Resource 注解是 J2EE 提供的,而 @Autowried 注解是 Spring 提供的,他们如果感兴趣可以去看一下他们的区别。

4、在Mapper接口上加@Component
@Mapper
@Component
public inteface CarUserMapper{}

加这个注解呢,主要是为了让欺骗IEDA,让它以为CarUserMapper也是一个Spring管理的Bean,这样子使用@Autowired注解注入也不会报错了。

5、使用构造函数注入(Spring4.x推荐)

相信大家都有使用Lombok这个神器了,我们可以利用他的注解@RequiredArgsConstructor来直接构建构造函数即可,不过我尝试过单单使用@AllArgsConstructor也是没问题的。
当然了,大家如果注入的依赖比较少或者闲得蛋疼,还是可以自己来写构造函数的。

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
//@AllArgsConstructor
public class ChargeServiceImpl implements ChargeService {

    private final RedisTemplate<String, Object> redisTemplate;
    private final CarUserMapper carUserMapper;
    private final ChargingMapper chargingMapper;
}

为什么Spring4.x之后就推荐使用构造函数注入呢?大家可以看看一篇文章:
Why field injection is evil
简单点总结一下就是:可以使依赖不可变、实例化时,会检查构造函数的参数是否为空、方便单元测试等等。

猜你喜欢

转载自www.cnblogs.com/Howinfun/p/11731826.html