2014-10-29 104 views
1

我正在嘗試使用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中初始化。但是,當我嘗試運行作業時,切入點不會被觸發。爲什麼會發生?有什麼方法可以解決它嗎?

回答

0

我最終結束了放棄AOP和我的前看點轉化成StepListener,工作正常。

1

您的execution()切入點匹配具有任意數量參數的方法調用。因此,您需要告知args()參數stepExecution相對於其他參數的位置,以便使其與多於一個參數的方法匹配,例如,

  • 第一個參數:args(stepExecution, ..)
  • 第二個參數:args(*, stepExecution, ..)
  • 第三個參數:args(*, *, stepExecution, ..)
  • 最後一個參數:args(.., stepExecution)
+0

Execute是我嘗試匹配的方法中唯一的參數,所以我會給第一個參數解決方案一個鏡頭。 – 2014-10-30 13:32:23

+0

沒有骰子。您的解決方案在本地測試中傳遞,但在部署到服務器時不起作用。 – 2014-10-30 15:07:17

+0

然後你有一個配置或類加載問題。 – kriegaex 2014-10-30 15:31:51