您的位置:首页 > 编程语言 > Java开发

Spring AOP代理时 ClassCastException: $Proxy0 cannot be cast to (类型转换错误)

2012-10-08 01:33 471 查看
今天犯了一个很低级的错误,估计是写了一天的代码,头被整昏了吧,在此纪念一哈哈;

以下是整个程序的源码。

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是一样的,所以导致类型转换错误,悲剧。警戒。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐