关于ssh的$Proxy0 cannot be cast to 报错

wrweirong 2012-06-25 11:19:24
报错: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包加入到项目中
第一种方法我已经做了,但是第二种方法我不会怎么弄。

贴上代码,大神帮忙啊,急急!!!
测试文件Test
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);

}

}

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: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>

...全文
41 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧