TransactionProxyFactoryBean 代理事务 $Proxy9 cannot be cast to异常解决方法
2016-04-12 19:04
363 查看
异常如下:
[com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor]-[ERROR] $Proxy9 cannot be cast to com.student.service.StudentService
java.lang.ClassCastException: $Proxy5 cannot
be cast to com.student.service.StudentService
解决方法:
导致出问题的代码片段:
//userService定义为接口类型!!! 不能是实现类类型
private static
UserServiceBean userService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext cat = new ClassPathXmlApplicationContext("beans.xml");
userService = (UserServiceBean) cat.getBean("userServiceBean");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
正确代码:
private static UserService userService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext cat = new ClassPathXmlApplicationContext("beans.xml");
userService = (UserService) cat.getBean("userServiceBean");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这主要代理需要用到接口
Ps: 所以通过这种代理的话,必须声明一个接口(例如Service),并在接口中声明实现类(例如: UserService)所需要的方法,这样 userService = (UserService)
cat.getBean("userServiceBean")
其中把 private static UserService userService 写成了 private static UserServiceBean userService
[com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor]-[ERROR] $Proxy9 cannot be cast to com.student.service.StudentService
java.lang.ClassCastException: $Proxy5 cannot
be cast to com.student.service.StudentService
解决方法:
导致出问题的代码片段:
//userService定义为接口类型!!! 不能是实现类类型
private static
UserServiceBean userService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext cat = new ClassPathXmlApplicationContext("beans.xml");
userService = (UserServiceBean) cat.getBean("userServiceBean");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
正确代码:
private static UserService userService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext cat = new ClassPathXmlApplicationContext("beans.xml");
userService = (UserService) cat.getBean("userServiceBean");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
这主要代理需要用到接口
Ps: 所以通过这种代理的话,必须声明一个接口(例如Service),并在接口中声明实现类(例如: UserService)所需要的方法,这样 userService = (UserService)
cat.getBean("userServiceBean")
其中把 private static UserService userService 写成了 private static UserServiceBean userService
相关文章推荐
- Ubuntu14.10下LAMP的环境配置
- 数据库自增字段是字符型的简单处理
- java.lang.NoClassDefFoundError: * : Landroid/support/v7/gridlayout/R$styleable 异常终极解决办法
- 分享某公司JAVA实习生招聘代码面的一道题
- 山东省第四届ACM大学生程序设计竞赛 Boring Counting 划分树
- AsynTask源码总结
- 20145238-荆玉茗 《Java程序设计》第7周学习总结
- bell-manford判负环模板
- jsp和exclipse还有myexclipse+mysql+sqlserver
- 一种夸域名web单点登录设计思路
- Net分布式系统之一:系统整体框架介绍
- Spring与Mybatis三种常用整合方法
- openfire 成功安装后无法启动,无法访问http://ip:9090
- Android自定义控件1
- NYOJ-23 取石子(一)
- Android(M) Telephony Framework SIM card recognization flow
- WebView弹出Alert 去掉Http:--网址Title
- mysql语句优化对比
- Java学习——运算顺序的详细挖掘(下)
- android6.0 adbd深入分析(二)adb驱动数据的处理、写数据到adb驱动节点