今天在带领学生做spring声明式事务时,部分同学的程序报错com.sun.proxy.$Proxy6 cannot be cast to。下面就详细介绍一下这个错误的原因及解决方法。
一、工程结构图
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.1.104:1521:icss" />
<property name="driverClass" value="oracle.jdbc.OracleDriver" />
<property name="user" value="scott" />
<property name="password" value="icss" />
<property name="initialPoolSize" value="3" />
<property name="maxPoolSize" value="10" />
<property name="minPoolSize" value="1" />
<property name="acquireIncrement" value="3" />
<property name="maxIdleTime" value="60" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="userDaoImpl" class="dao.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="userServiceImpl" class="service.UserServiceImpl">
<property name="user" ref="userDaoImpl"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"></property>
</bean>
<tx:advice id="txTransactionManager" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
<tx:method name="insert*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
<tx:method name="find*" read-only="true" />
<tx:method name="read*" read-only="true" />
<tx:method name="load*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="within(service.*)" id="txPointCut"></aop:pointcut>
<aop:advisor advice-ref="txTransactionManager" pointcut-ref="txPointCut"></aop:advisor>
</aop:config>
</beans>
package config;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.IUserService;
import service.UserServiceImpl;
public class TestUser {
/**
* @param args
*/
@Test
public void insert(){
ApplicationContext ac=new ClassPathXmlApplicationContext("config/applicationContext.xml");
IUserService user=(UserServiceImpl) ac.getBean("userServiceImpl");
user.insertBatch();
}
}
二、错误原因
Spring AOP实现方式有两种,一种使用JDK动态代理,另一种通过CGLIB来为目标对象创建代理。如果被代理的目标实现了至少一个接口,则会使用JDK动态代理,所有该目标类型实现的接口都将被代理。若该目标对象没有实现任何接口,则创建一个CGLIB代理,创建的代理类是目标类的子类。
显然,本工程中是通过JDK动态代理来实现AOP的。
三、解决方法
1.在applicationContext.xml中增加下面内容,来强制使用CGLIB创建代理。
<strong><aop:aspectj-autoproxy proxy-target-class="true"/> </strong>
<aop:config>
<aop:pointcut expression="within(service.*)" id="txPointCut"></aop:pointcut>
<aop:advisor advice-ref="txTransactionManager" pointcut-ref="txPointCut"></aop:advisor>
</aop:config>
public void insert(){
ApplicationContext ac=new ClassPathXmlApplicationContext("config/applicationContext.xml");
IUserService user=<strong>(IUserService)</strong> ac.getBean("userServiceImpl");
user.insertBatch();
}