写项目的时候遇到这个问题,这里可以看到,上面的红色警告在提示我们,找不到UserMapper 这个类型的 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;
}
所以就改呗
因为@Resource 注解是 J2EE 提供的,而 @Autowried 注解是 Spring 提供的。这样就不会给红色警告了。