Spring代理/aop异常

问题描述:

Failed to convert property value of type 'com.sun.proxy.$Proxy6 implementing org.springframework.jdbc.core.JdbcOperations,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.core.DecoratingProxy' to required type 'org.springframework.jdbc.core.JdbcTemplate' for property 'jdbcTemplate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'com.sun.proxy.$Proxy6 implementing org.springframework.jdbc.core.JdbcOperations,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.core.DecoratingProxy' to required type 'org.springframework.jdbc.core.JdbcTemplate' for property 'jdbcTemplate': no matching editors or conversion strategy found

原因是Spring通常会默认使用JDK代理模式这样的弊端是很显然的,返回对象必须是接口

解决方案: 

<!-- 默认使用CGLIB代理(设为true) -->

<aop:aspectj-autoproxy proxy-target-class="true"/>

之后又出现问题

Could not generate CGLIB subclass of class [class com.mchange.v2.c3p0.ComboPooledDataSource]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class com.mchange.v2.c3p0.ComboPooledDataSource

这个问题说明了现在确实是CGLIB代理模式了但是出现的这个问题还真的是困扰了我好久之后发现是我的aop把它过滤了导致的

 <aop:config>
        <aop:pointcut expression="execution(* *..*.*(..))" id="pointcut"/>
        <aop:aspect ref="work1">
        <aop:around method="around" pointcut-ref="pointcut"/>
        </aop:aspect>
 </aop:config

然后我将过滤范围缩小就一切正常了  

在这里提一点就是在spring中使用JdbcTemplate时是多线程的,每调用一次就好多出一个JdbcTemplate对象所以有时会报空指针异常或者是没有找到DataSource对象

解决方案同样是将aop过滤范围缩小,因为经过aop过滤了之后又会产生一个JdbcTemplate对象所以对数据库的操作会产生空指针,或者找不到数据源的错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值