首页 > 学院 > 开发设计 > 正文

测试 Spring aop时出现java.lang.ClassCastException的错误

2019-11-14 22:35:58
字体:
来源:转载
供稿:网友
测试 SPRing aop时出现java.lang.ClassCastException的错误

代码如下:

public class UserTest {        private static UserServiceImpl userService ;        @BeforeClass    public static void iniUserService(){        applicationContext applicationContext = new ClassPathxmlApplicationContext("applicationContext.xml") ;        userService = (UserServiceImpl) applicationContext.getBean("userService") ;    }        @Test    public void test() {        User user = new User(3,"shpengfish@Gmail.com","ysp","123456","yushipeng",new Date()) ;        System.out.println(user);        userService.saveEntity(user) ;    }}

  junit 报错

java.lang.ClassCastException: com.sun.proxy.$Proxy13 cannot be cast to com.ysp.surveypark.service.impl.UserServiceImpl  后来上网搜索了下该问题,是因为 UserServiceImpl 实现了 UserService 接口,而该测试类中 却是使用 UserServiceImpl 声明的,这并没有体现出面向接口编程的思想,而 aop 使用的JDK 代理是基于接口的,因此报错,将实现类改为接口类型即可。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表