java.lang.NoSuchMethodException: com.sun.proxy.$Proxy58.list错误解决办法
java.lang.NoSuchMethodException:com.sun.proxy.$Proxy58.list错误解决办法
玩web的SSH总会有些令你意想不到的exception,这里其中有很多事自己不小心,或者马虎所造成。因此,解决的方案会各有不同,别人出现的异常解决方案对你的可能无效,就像上面的我报的异常一样,百度了很多很多次,给我的答案无非就是在aop上加上一句,但是非常抱歉,我加上去无效!所以还是那句话,对于自己的异常,还是要自己解决。
首先说明一下,我这次的练习的ssh结构和异常所报的原因。
用的框架式struts2.2+hibernate4.2+spring4.0,容器室tomcat7.0.在Action中,我写了一个基类BaseAction,继承了ActionSupport,实现了ModelDriven接口,代码如下:
---------- @Component @Transactional ---------- publicabstractclassBaseAction<T>extendsActionSupportimplementsModelDriven<T>{ privatestaticfinallongserialVersionUID=2719688501307297741L; @Resource(name="roleServiceImpl") protectedRoleServiceroleService; @Resource(name="departmentService") protectedDepartmentServicedepartmentService; @Resource(name="userService") protectedUserServiceuserService; protectedTmodel; publicBaseAction(){ ParameterizedTypept=(ParameterizedType)this.getClass().getGenericSuperclass(); @SuppressWarnings("unchecked") Class<T>clazz=(Class<T>)pt.getActualTypeArguments()[0]; try{ model=clazz.newInstance(); }catch(Exceptione){ thrownewRuntimeException(e); } } publicTgetModel(){ returnmodel; } }
这样做的好处是,每个组件对应的Action中只需要告诉BaseAction自己要实现的泛型,然后只需要完成自己的逻辑。对于每个Action要实现ActionSupport来讲,这是一项基本的且容易想到的方案。
然后,在每个Action中调用service组件实现自己的功能,在每个Action中都使用了@Component(“departmentAction”),@Scope(“prototype”)注解,但是练习一测试,就会出现Java.lang.NoSuchMethodException:com.sun.proxy.$Proxy58.list()异常,检查之后发现,原因是:
BaseAction是抽象的,在上面加了@Transactional是没有意义的,
而@Transactional就是有spring管理,spring管理的对象要生成代理,而对于一个抽象的类,是不能被new成对象的,只有一个抽象类的具体实现类才有可能是被spring代理。
所以,去掉BaseAction上面的@Transactional,就是本题的原因了。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!