报错:Exception in thread "main" java.lang.ClassCastException: $Proxy0 cannot be cast to com.wr.domain.User
度娘了,说解决方法有两种 1、给service类添加一个接口iService,让service类实现它,则创建代理类时使用JDK动态代理就不会出现问题 2、设置beanNameAutoProxyCreator的proxyTargetClass属性为true,意思是强制使用CGLIB代理,前提是你已经将CGLIB包加入到项目中
第一种方法我已经做了,但是第二种方法我不会怎么弄。
贴上代码,大神帮忙啊,急急!!!
- Java code
public class Test { /** * @param args */ public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); User u = (User)ac.getBean("userServiceImpl"); u.setId("101"); u.setPwd("101"); UserServiceImpl userServiceImpl = new UserServiceImpl(); userServiceImpl.addUser(u); }}[color=#FF6600]applicationContext.xml[/color]<?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: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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <!-- applicationContext.xml事务管理器的配置 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <!-- 注入applicationContext.xml中配置的sessionFactory --> <ref bean="sessionFactory" /> </property> </bean> <!-- dao --> <bean id="UserDaoImpl" class="com.wr.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- service --> <bean id="userServiceImpl" class="com.wr.service.impl.UserServiceImpl"> <property name="userDao" ref="UserDaoImpl"></property> </bean> <!-- action --> <bean name="addUserAction" class="com.wr.action.addUserAction"> <property name="userServiceImpl" ref="userServiceImpl"></property> </bean> <!-- 事务通知的配置 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 对do开头的方法要求事务 --> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <!-- 对其他方法的配置 --> <tx:method name="*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <!-- 将事务通知和切入点组合 --> <aop:config> <aop:pointcut expression="execution(* com.wr.service.impl.*.*(..))" id="pc" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="pc" /> </aop:config></beans>