java.lang.NoSuchMethodException: $Proxy

來源:互聯網
上載者:User

今天遇到這麼一個異常,原來是對action進行了代理,這個是在spring的設定檔中的。所以,修改掉就正常了。

 

如何修改呢?

1. 加入代理的話,那麼action 中就要有如下方法。

   <property name="transactionAttributes">
            <props>
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>

2. 不配置代理。。

3. 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對應的方法中修改不會被實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.