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层接口的用法,想知道原因?为什么可以这么用?