在使用idea的@Autowired注解注入Mapper文件时,有时会标红,但是不影响项目运行,那么是什么原因呢
原因
先看一下@Autowired源码:
@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;
默认值为true说明@Autowired注解必须要注入一个非空的对象,@Autowired注解的包,来自于:
import org.springframework.beans.factory.annotation.Autowired;
@Mapper的包来自于:
import org.apache.ibatis.annotations.Mapper;
总结报错的原因:@Autowired注解需要一个非空的对象,而@Autowired是SpringFramework的,但是它引入的对象是Mybat