@Repository注解,可以用在dao层接口?

spring测试代码:

//daoimpl
public class TeacherDao implements ITeacherDao {

}
//dao层接口,使用repository注解
@Repository
public interface ITeacherDao {
}

//serviceImpl
@Service
public class TeacherService implements ITeacherService {

    @Autowired
    ITeacherDao teacherDao;
}
//service
public interface ITeacherService {

}

//测试代码
public class TeacherTest {

    private ITeacherService teacherService;
    private ApplicationContext applicationContext;

    @Before
    public void init(){
        applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        teacherService = applicationContext.getBean("teacherService", ITeacherService.class);
    }

    @Test
    public void testIOC(){
        System.out.println(teacherService);
    }
}

报错:No qualifying bean of type 'com.aipande.dao.ITeacherDao' available。

如果将Repository注解放在dao层实现类上就可以正常注入。

通过这个例子,我认为Repository注解只能作用在类上,不能作用在接口上。

但是在学习springBoot整合mybatis的时候,看到有将repository注解放在dao层接口的用法,想知道原因?为什么可以这么用?

我就想问问你用了 Mybatis 吗?没有 Mybatis 的话,你自己干的不就是 Mybatis 的事情吗?Mybatis 就是帮你把 DAO 接口通过代理生成它的实现类然后在代理里面执行 sql。

可以