2012-04-08 52 views
2
public abstract class AService<T> { 
    public T needsToBeAdvised(T param) { 
    T result = doSomething(param); 
    return result; 
    } 
} 

@Service 
public class BService extends AService<B> { 
    @Override 
    public T needsToBeAdvised(T param) { 
    return super.needsToBeAdvised(param); 
    } 
} 

@Service 
public class CService extends AService<C> {} 

// (B & C implement an interface AType) 

@Component 
@Aspect 
public class MyAspect { 
    @Pointcut("execution(* package.AService+.needsToBeAdvised(*))") 
    private void aNeedToBeAdvised() {} 

    @AfterReturning(pointcut="aNeedToBeAdvised()", returning="param") 
    public void interceptNeedsToBeAdvised(JoinPoint joinPoint, AType param) { 
    // some action 
    } 
} 

鑑於此設置沒有覆蓋超類方法:Spring AOP的 - 建議在子類中

bService.needsToBeAdvised(bParam) //is intercepted 

但是,

cService.needsToBeAdvised(cParam) //is NOT. 

如何做到這一點沒有覆蓋在CServiceneedsToBeAdvised()

編輯:

我要補充一點,BServiceCService都在同一個包。

如果我改變我的觀點切到以下幾點:

@Pointcut("execution(* package.CService.needsToBeAdvised(*))") 

cService.needsToBeAdvised(cParam) //is still not intercepted 

它的工作的唯一方法是,如果我在CService

回答

1

覆蓋needsTobeAdvised()在同一包裝中的所有服務?從您的示例代碼給出,我懷疑AServiceBServicepackage包中,但CService要在另一個包中。如果確實服務在不同的包,你有一些選擇:

  • 移動,以使它們在同一個包
  • 更改您切入點是比較通用的,如"execution(* *.A+.needsToBeAdvised(*))
  • 添加更多的切入點
+0

感謝您的答覆。他們在同一個包裏。見上面的編輯。 – mantithetical 2012-04-08 19:28:56