ssh java.lang.NoSuchMethodException: $Proxy

来源:互联网 发布:安禄山 知乎 编辑:程序博客网 时间:2024/04/30 23:03

首先我保证了在没加AOP实现之前,代码是能正常运行的.
然后学习SSH使用AOP在action的方法前添加权限控制,该文就是记录出现的错误信息..

先说明用xml注解AOP

<aop:config proxy-target-class="true">  
    <aop:aspect ref="rightFilter">  
        <aop:before method="rightFilter" pointcut="execution(* com.exdoit.action.Main.mainPage())"></aop:before>  
        <aop:after method="actionAfter" pointcut="execution(* com.exdoit.action.Main.mainPage())"></aop:after>  
    </aop:aspect>  
</aop:config> 
<aop:config proxy-target-class="true">
 <aop:aspect ref="rightFilter">
  <aop:before method="rightFilter" pointcut="execution(* com.exdoit.action.Main.mainPage())"></aop:before>
  <aop:after method="actionAfter" pointcut="execution(* com.exdoit.action.Main.mainPage())"></aop:after>
 </aop:aspect>
</aop:config>

起先是使用的annotation注解,然后出问题找不到 proxy-target-class 怎么注解就转成XML了.

java.lang.NoSuchMethodException: $Proxy...
先是代理对象里找不到方法.问题是解决了.

spring中代理对象的生成方式有2种,

1:利用jdk中的proxy实现,要求我们的被代理对象必须要去实现一个代理接口,代理对象和被代理对象本质是是实现了统一接口的两个对象

2:利用cglib来实现.被代理对象不需要去实现一个代理接口,被代理类和代理类之间本质是父子类的关系

proxy-target-class="true" 是指定由cglib来实现实现代理.

解决方法(2种):

1.取消继承 com.opensymphony.xwork2.ActionSupport 放弃这种方法
2.配置文件里的 <aop:config proxy-target-class="true" >....</aop:config>
继续AOP权限控制测试又报如下错误
Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
没有添加 cglib.jar 的包...你说没包你就跟下面的 NoClassDefFoundError 提示一样不得了..还自己来个什么 Add CGLIB to the class...搞半天才明白是少包了.

...继续

nested exception is java.lang.NoClassDefFoundError : Could not initialize class...

根据提示找包就是了.简单的问题.

运行结果是 AOP 拦截成功实现,但是action的变量没有输出结果...
如 public String nextPath;直接使用public声明取不到值(不用AOP前是可以直接通过public声明取值的).需要为变量加一个 get 方法:
public String getNextPath(){return nextPath;}
当请求一个变量的时候,转为寻找这个变量get方法的返回值.

估计应该是AOP的动态代理机制导致公共变量在action对应的方法中修改不会被实现


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/kvgnt/archive/2011/02/20/6196726.aspx

原创粉丝点击