今天犯了一个很低级的错误,估计是写了一天的代码,头被整昏了吧,在此纪念一哈哈;
以下是整个程序的源码。
public class BrandTest {
private static ApplicationContext context;
private static BrandService brandService;
@BeforeClass
public static void beforeClass(){
try {
brandService = (BrandService) new ClassPathXmlApplicationContext("beans.xml").getBean("brandService");
} catch (BeansException e) {
e.printStackTrace();
}
}
@Test
public void testSave(){
brandService.save(new Brand("椰树瑜伽","/images/luoshu/gdgd.gif"));
}
}
我的类结构还有:
BrandDao
BrandDaoImpl
BrandService
BrandServiceImpl
大家应该都明白。
BrandService 是一个接口,目的.getBean("brandService")获取到的bean是BrandService的实现(也就是BrandServiceImpl),没想到我却去实现BrandDao了,也并没有报错,因为目前BrandDao和BrandService是一样的,所以导致类型转换错误,悲剧。警戒。。