我正在嘗試爲接口中聲明的方法添加around
建議。Around接口方法的建議
我的封裝結構如下:
- 樣品
- sample.ifaces(這個包裝具有SampleInterface其具有方法聲明sampleOperation)
- sample.ifaces.impl(這個包裝具有SampleImplementation它實現sampleOperation)
- sample.aspect(類SampleAspect)
在薩姆pleAspect我試圖添加左右的建議
@Component
@Configuration
@EnableAspectJAutoProxy
@Aspect
public class SampleAspect {
//@Pointcut("execution(public * sample.ifaces.impl.SampleImplementation .*(..))")
//@Pointcut("within(sample.ifaces.SampleInterface+) && execution(* sample*(..))")
//@Pointcut("execution(public * sample.ifaces.SampleInterface+.sample*(..))")
//@Pointcut("execution(* sample.ifaces.SampleInterface.*(..))")
//@Pointcut("execution(* sample.ifaces.SampleInterface+.*(..))")
@Pointcut("execution(* sample.ifaces.SampleInterface.sample*(..))")
public void retry() {
}
@Around("retry()")
private void retryHandler(ProceedingJoinPoint pjp){
Object o = null;
try {
logger.info("before exec");
o = pjp.proceed();
} catch (Exception e) {
logger.info("exception in exev");
} catch (Throwable e) {
logger.error("unknown");
}
}
}
我試過許多組合,使其工作,但它不工作。我需要在接口級別爲方法SampleOperation定義Pointcut,以及何時通過超類引用執行實現的方法,建議應該被執行。
**更新**
我進一步分析這一點。我的新的包的結構是:
- 樣品
- sample.config(此封裝具有ConfigBean其在appContext.xml定義爲豆)
- sample.ifaces(這個包裝具有SampleInterface具有聲明sampleOperation方法)
- sample.ifaces.impl(這個包裝具有SampleImplementation它實現在應用程序上下文bean中定義sampleOperation)
- sample.aspect(類SampleAspect)
ConfigBean
bean是從appContext初始化的。 初始化方法包含初始化SampleInterface
引用的代碼。
SampleInterface
不是春豆。我試過不同的組合。當我使用
@Pointcut("within(sample.config.*)") //-> Does work
當我使用
@Pointcut("within(sample.ifaces.*)") //-> Does not work
執行點式切口不要爲我工作。他們只是編譯成功,但不會被調用。
對於初學者你'@ Around'方法是錯誤的執行。它必須始終返回Object,並且您應始終返回結果。像'執行(* sample.ifaces.SampleInterface +。*(..))'應該做的伎倆。你是否確定你的方面適用於你的配置? –
增加了問題的進一步exaplanation。 –
我不明白你是否想用Spring管理bean做些事情。如果沒有,它將永遠不會工作,因爲它只能用於sprnig託管bean,除非使用像加載時間或編譯時間之類的東西。 –