2014-09-05 63 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