Search in sources :

Example 1 with AfterThrowing

use of org.aspectj.lang.annotation.AfterThrowing in project spring-framework by spring-projects.

the class ReflectiveAspectJAdvisorFactory method getAdvice.

@Override
public Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut expressionPointcut, MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName) {
    Class<?> candidateAspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass();
    validate(candidateAspectClass);
    AspectJAnnotation<?> aspectJAnnotation = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod);
    if (aspectJAnnotation == null) {
        return null;
    }
    // Check that it's an AspectJ-annotated class
    if (!isAspect(candidateAspectClass)) {
        throw new AopConfigException("Advice must be declared inside an aspect type: " + "Offending method '" + candidateAdviceMethod + "' in class [" + candidateAspectClass.getName() + "]");
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Found AspectJ method: " + candidateAdviceMethod);
    }
    AbstractAspectJAdvice springAdvice;
    switch(aspectJAnnotation.getAnnotationType()) {
        case AtBefore:
            springAdvice = new AspectJMethodBeforeAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            break;
        case AtAfter:
            springAdvice = new AspectJAfterAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            break;
        case AtAfterReturning:
            springAdvice = new AspectJAfterReturningAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            AfterReturning afterReturningAnnotation = (AfterReturning) aspectJAnnotation.getAnnotation();
            if (StringUtils.hasText(afterReturningAnnotation.returning())) {
                springAdvice.setReturningName(afterReturningAnnotation.returning());
            }
            break;
        case AtAfterThrowing:
            springAdvice = new AspectJAfterThrowingAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            AfterThrowing afterThrowingAnnotation = (AfterThrowing) aspectJAnnotation.getAnnotation();
            if (StringUtils.hasText(afterThrowingAnnotation.throwing())) {
                springAdvice.setThrowingName(afterThrowingAnnotation.throwing());
            }
            break;
        case AtAround:
            springAdvice = new AspectJAroundAdvice(candidateAdviceMethod, expressionPointcut, aspectInstanceFactory);
            break;
        case AtPointcut:
            if (logger.isDebugEnabled()) {
                logger.debug("Processing pointcut '" + candidateAdviceMethod.getName() + "'");
            }
            return null;
        default:
            throw new UnsupportedOperationException("Unsupported advice type on method: " + candidateAdviceMethod);
    }
    // Now to configure the advice...
    springAdvice.setAspectName(aspectName);
    springAdvice.setDeclarationOrder(declarationOrder);
    String[] argNames = this.parameterNameDiscoverer.getParameterNames(candidateAdviceMethod);
    if (argNames != null) {
        springAdvice.setArgumentNamesFromStringArray(argNames);
    }
    springAdvice.calculateArgumentBindings();
    return springAdvice;
}
Also used : AopConfigException(org.springframework.aop.framework.AopConfigException) AspectJAfterThrowingAdvice(org.springframework.aop.aspectj.AspectJAfterThrowingAdvice) AspectJAroundAdvice(org.springframework.aop.aspectj.AspectJAroundAdvice) AspectJMethodBeforeAdvice(org.springframework.aop.aspectj.AspectJMethodBeforeAdvice) AspectJAfterAdvice(org.springframework.aop.aspectj.AspectJAfterAdvice) AbstractAspectJAdvice(org.springframework.aop.aspectj.AbstractAspectJAdvice) AspectJAfterReturningAdvice(org.springframework.aop.aspectj.AspectJAfterReturningAdvice) AfterReturning(org.aspectj.lang.annotation.AfterReturning) AfterThrowing(org.aspectj.lang.annotation.AfterThrowing)

Example 2 with AfterThrowing

use of org.aspectj.lang.annotation.AfterThrowing in project wechat by dllwh.

the class SystemLogAspect method doAfterThrowing.

/**
 * @方法描述:标注该方法体为异常通知,当目标方法出现异常时,执行该方法体 <br>
 *                                     用于拦截记录异常日志
 * @param joinPoint
 * @param e
 */
@AfterThrowing(pointcut = "controllerlogPointCut()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
    if (logger.isDebugEnabled()) {
        logger.debug("=========执行异常通知===============");
    }
    try {
        HttpServletRequest request = WebUtilHelper.getHttpServletRequest();
        String ip = IpUtilHelper.getClientIP(request);
        String browser = UserAgent.parseUserAgentString(request.getHeader("User-Agent")).getBrowser().getName();
        LogManager.getInstance().executeLog(LogTaskFactory.operateLog(joinPoint, 0, e, null, ip, browser));
    } catch (Exception e2) {
        e2.printStackTrace();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AfterThrowing(org.aspectj.lang.annotation.AfterThrowing)

Aggregations

AfterThrowing (org.aspectj.lang.annotation.AfterThrowing)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 AfterReturning (org.aspectj.lang.annotation.AfterReturning)1 AbstractAspectJAdvice (org.springframework.aop.aspectj.AbstractAspectJAdvice)1 AspectJAfterAdvice (org.springframework.aop.aspectj.AspectJAfterAdvice)1 AspectJAfterReturningAdvice (org.springframework.aop.aspectj.AspectJAfterReturningAdvice)1 AspectJAfterThrowingAdvice (org.springframework.aop.aspectj.AspectJAfterThrowingAdvice)1 AspectJAroundAdvice (org.springframework.aop.aspectj.AspectJAroundAdvice)1 AspectJMethodBeforeAdvice (org.springframework.aop.aspectj.AspectJMethodBeforeAdvice)1 AopConfigException (org.springframework.aop.framework.AopConfigException)1