1

我是 Spring AOP 的新手。

当我的应用程序抛出异常时,我已经为我的应用程序中的所有方法编写了方面建议。(我已经用 grails 尝试过。)

异常服务.groovy

@Aspect
public class ExceptionService {

 def logMessage(def message){
   def expInstance = new LogMessage(message:message)
   expInstance.save(flush:true)
 }

 @Pointcut("execution(* com.mypackage..* (..))")
 def allActions(){}

 @AfterThrowing(pointcut="allActions()", throwing="e")
 def allActionAdvice(JoinPoint joinPoint, Throwable e){
   def className = joinPoint.getTarget().getClass().getSimpleName()
   logMessage(className+"\n"+e.message)
 }

 @Pointcut("execution(* org.apache.commons.logging.Log.error(..))")
 def logErrorActions(){}

 @Before(value = "logErrorActions()")
 def logErrorActionAdvice(JoinPoint joinPoint){
   def className = joinPoint.getTarget().getClass().getSimpleName()
   logMessage("Error in "+className)
 }

}

资源.groovy

beans = {
 xmlns aop:"http://www.springframework.org/schema/aop"
 aspectBean(com.mypackage.services.ExceptionService)
 aop.config("proxy-target-class":true) {

 }
}

allActionAdvice()在这里,它适用于应用程序内部发生的所有异常。但它不适用于log.error(logErrorActionAdvice()).

我在谷歌上对其进行了研究,它表明问题在于 AOP 和第三方依赖,如编织。所以,我需要做方面编织(编译时编织)。但我没有找到任何好的例子。

我需要对我的 Grails 应用程序进行哪些更改以使用我的方面的编译时编织,或者我还需要做些什么?

4

0 回答 0