问题描述:
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对象所以对数据库的操作会产生空指针,或者找不到数据源的错误