2014-09-05 40 views
1

這是我在stackoverflow上的第一篇文章... 好吧,在這裏。 我有一個自定義Spring AOP的註解這對於該方法當我拆分方法時,彈簧AOP沒有被應用

@testInterceptor 
public MyObjList getMyObjList(List qlist,Context cntxt){ 
//some processing 
List<MyObj> myObjList= getMyObjs(qlist,cntxt); 
//Some more processing 
return myObjList; 
} 


public List<MyObj> getMyObjs(List qlist,Context cntxt){ 
List<MyObj> myObjList= new ArrayList<MyObj>(); 
//Some more processing 
return myObjList; 
} 

我意識到,這個註釋實際上應該是在getMyObjs()方法工作正常。 所以我將註釋移到了getMyObjs(),但由於某種原因,現在這個方面沒有被應用。 我不知道爲什麼。

@testInterceptor 
public List<MyObj> getMyObjs(List qlist,Context cntxt){ 
List<MyObj> myObjList= new ArrayList<MyObj>(); 
//Some more processing 
return myObjList; 
} 

回答

1

由於Spring是如何使用AOP,爲了@testInterceptorgetMyObjs工作,該方法需要從外部類調用。從getMyObjList調用它不會涉及攔截器。

結賬this博客文章瞭解更多詳情。

爲了澄清什麼,我用上面的例子:

比方說,你有另一個類

class Foo { 

    @Autowired 
    private MyObjList myObjList; 

    //this will invode the interceptor 
    public void willWork() { 
    myObjList.getMyObjs(); 
    } 

    public void willNotWork() { 
    myObjList.getMyObjList(); //will not invoke interceptor since `getMyObjs` is being invoked from inside the class that it's defined 
    } 

} 
+0

感謝link.I讀它......但沒有很明白它:(逸岸,原本無論在getmyobjs()中做了什麼,它都是原始方法的一部分,我只是將邏輯分成了另一種方法。 – 2014-09-05 15:58:16

+0

@geonad,當你說「該方法需要從課堂外調用」時,你是什麼意思?方法是同一類的一部分 – 2014-09-05 16:18:23

+0

@MonaD我給我的回答添加了一個例子 – geoand 2014-09-05 16:26:54