2012-03-15 88 views
2

我不能让我的afterThrowing Spring AOP的意见,以火, 我做了点现在削减尽可能通用,它仍然不火AfterThrowing建议不工作的Spring AOP

我希望这只是一个可怜的切入点但我不明白为什么,我将不胜感激,如果任何人都可以看到为什么

咨询

//一般例外

@AfterThrowing(value = "execution(* *(..)) throws Exception", throwing = "exception") 
    public void loggingGenericException(JoinPoint joinPoint, Exception exception) { 
     String classMethod = this.getClassMethod(joinPoint); 

     String stackTrace = ""; 
     for (StackTraceElement element : exception.getStackTrace()) { 
      stackTrace += element.toString() + "\n"; 
     } 

     String exceptionMessageAndStackTrace = exception.getMessage() + "\n" + stackTrace; 

     if (exception instanceof EmptyResultSetException) { 
      this.infoLevelLogging(joinPoint, classMethod); 
     } else { 

      this.errorLevelLogging(joinPoint, classMethod, exceptionMessageAndStackTrace); 
     } 
    } 

方法如果我运行一个测试,应建议

public void getStudentTranscript(String studentId) throws RestClientException,IllegalArgumentException{ 


     if (!this.serviceUrl.isEmpty()) { 

      if(studentId.isEmpty()) 
      { 
       throw new IllegalArgumentException("studentId empty"); 
      } 

      this.transcript = (Transcript) super.getForObject(this.serviceUrl,Transcript.class, studentId); 




     } else { 

      throw new IllegalArgumentException("url is empty"); 
     } 

} 

,以检查它应用于它是不工作的测试看起来像这样

@Test 
public void testLoggingFiredOnExceptionInTranscriptRepository() throws Exception 
{ 
    Log log; 
    log = mock(Log.class); 
    when(log.isErrorEnabled()).thenReturn(true); 

    try { 
     loggingAspects.setLogger(log); 
     transcriptRepository.setServiceUrl(""); 
     transcriptRepository.getStudentTranscript("12345"); 
    } catch (RuntimeException e) { 
     System.out.println("e = " + e); 
     verify(log, times(1)).isErrorEnabled(); 
     verify(log, times(1)).error(anyString()); 

    } 
} 

制出显示了异常解雇

任何人都可以提供任何建议(双关意图):-)

+0

已解决 - 问题与测试设置 – 2012-03-26 15:20:42

+0

你能分享什么是解决方案,我面临类似问题 – Programmer 2017-05-10 14:58:20

回答

0

你把弹簧配置文件中的<aop:aspectj-autoproxy />元素?否则,AOP注释将不被解释。

仅供参考,在阅读完您的问题后,我自行创建了一个示例项目,@AfterThrowing注释正好符合它的要求。

+0

感谢您的回答和努力回头看我已经在我的测试用例设置中声明了一个新实例 - 所以它没有运行在AOP上下文中 – 2012-03-26 15:20:12