我正在嘗試使用AspectJ AOP攔截對Spring批處理步驟執行的調用,並建議要登錄的文件。AOP切入點匹配Spring Step.execute(...)方法
繼this example的配置,我的切入點是這樣的:
@Before("execution(* org.springframework.batch.core.Step.execute(..)) && " + "args(stepExecution)")
public void setupLogging(Object stepExecution) {...}
@After("execution(* org.springframework.batch.core.Step.execute(..))")
public void tearDownLogging() {...}
使用下面的測試(和一個類似的,當我推倒我的記錄),切入點比賽,但不要似乎當我嘗試部署它們時工作。
@Test
public void testSetupLoggingMatcher() throws NoSuchMethodException, SecurityException {
java.lang.reflect.Method method = LoggingAspect.class.getMethod("setupLogging", Object.class);
Annotation[] annotations = method.getDeclaredAnnotations();
boolean matched = false;
for (Annotation annotation: annotations) {
if (annotation.annotationType() == org.aspectj.lang.annotation.Before.class) {
org.aspectj.lang.annotation.Before beforeAnnotation = (org.aspectj.lang.annotation.Before) annotation;
String pointcutstring = beforeAnnotation.value();
PointcutParser pointcutParser =
PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingContextClassloaderForResolution();
Collection<PointcutParameter> parameters = new ArrayList<PointcutParameter>();
parameters.add(new PointcutParameter() {
@Override
public Class getType() {
return StepExecution.class;
}
@Override
public String getName() {
return "stepExecution";
}
@Override
public Object getBinding() {
return mockStepExecution;
}
});
PointcutExpression pointcut =
pointcutParser.parsePointcutExpression(pointcutstring, LoggingAspect.class,
parameters.toArray(new PointcutParameter[0]));
ShadowMatch match = pointcut.matchesMethodExecution(Step.class.getMethod("execute", StepExecution.class));
matched = matched || match.alwaysMatches();
}
}
assertTrue("No pointcuts on setupLogging matched Step.execute(StepExecution.class)", matched);
}
我已驗證我的切入點匹配Step接口,並且我的Aspect正在ApplicationContext中初始化。但是,當我嘗試運行作業時,切入點不會被觸發。爲什麼會發生?有什麼方法可以解決它嗎?
Execute是我嘗試匹配的方法中唯一的參數,所以我會給第一個參數解決方案一個鏡頭。 – 2014-10-30 13:32:23
沒有骰子。您的解決方案在本地測試中傳遞,但在部署到服務器時不起作用。 – 2014-10-30 15:07:17
然後你有一個配置或類加載問題。 – kriegaex 2014-10-30 15:31:51