在学习spring中,发现一段代码 定义接口 public interface BrandDao { @Select("select * from brand where id = #{id}") //配置数据库字段和模型实体类属性映射 @Results({ @Result(column = "brand_name", property = "brandName"), @Result(column = "company_name", property = "companyName") }) Brand findById(Integer id); } 定义一个service类 @Service public class BrandService { @Autowired private BrandDao brandDao; public Brand findById(Integer id) { return brandDao.findById(id); } } 执行方法 public class App { public static void main(String[] args) { ApplicationContext ioc = new AnnotationConfigApplicationContext(SpringConfig.class); BrandService brandService = ioc.getBean(BrandService.class); Brand brand = brandService.findById(2); System.out.println(brand); } } 上面代码可以正常运行,就是BrandDao接口他并没有实现类,为什么可以通过@Autowired进行注入? PS:应该是SpringConfig代码的这段已经有bean了: //定义bean,返回MapperScannerConfigurer对象 @Bean public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer msc = new MapperScannerConfigurer(); msc.setBasePackage("com.test.dao");//BrandDao接口属于这个包下 return msc; }