com.sun.proxy.$Proxy25 cannot be cast to com.levi.service.impl.ProductServiceImpl和proxy-target-class

刚刚写一个项目的时候出现了一个错误 com.sun.proxy.$Proxy25 cannot be cast to com.levi.service.impl.ProductServiceImpl,之前报错的代码为:

ProductServiceImpl p=(ProductServiceImpl) applicationContext.getBean("productService");
上面的ProductServiceImpl类是一个实现的ProductService接口的。利用spring ioc获取这个productService  bean的时候,不能转换为 ProductServiceImpl,而只能转为它的父接口ProductService。查了很多资料,发现是Spring AOP的一个配置文件问题。proxy-target-class这个参数。将spring配置文件改为下面就行了:

   <!-- 配置事务切面 -->  
    <aop:config proxy-target-class="true">  
        <aop:pointcut id="serviceOperation"  
            expression="execution(* com.levi.service..*.*(..))" />  
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
    </aop:config>  
  
   <!-- 自动扫描加载构建bean -->  
    <context:component-scan base-package="com.levi" />  
proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。如果proxy-target-class 属性值被设置为true,那么基于类的代理将起作用(这时需要cglib库)。如果proxy-target-class属值被设置为false或者这个属性被省略,那么标准的JDK 基于接口的代理将起作用。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值