spring 切面用例

import java.lang.reflect.Method;

import javax.inject.Named;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import com.jcabi.log.Logger;

/**
 * @author zhechen Log Aspect for method
 */
@Aspect
@Named
public class MethodLogger {

	@Pointcut("execution(* *(..)) && @annotation(com.jcabi.aspects.Loggable)")
	public void logMethod() {
	}

	@Before(value = "logMethod()")
	public void startMethodLog(JoinPoint joinPoint) throws Throwable {
		final Method method = ((MethodSignature) joinPoint.getSignature())
				.getMethod();
		final Class clazz = ((MethodSignature) joinPoint.getSignature())
				.getDeclaringType();
		long start = System.currentTimeMillis();
		Logger.debug(clazz, "invoke %s at %s", method, start);
	}

	@After(value = "logMethod()")
	public void endMethodLog(JoinPoint joinPoint) throws Throwable {
		final Method method = ((MethodSignature) joinPoint.getSignature())
				.getMethod();
		final Class clazz = ((MethodSignature) joinPoint.getSignature())
				.getDeclaringType();
		long end = System.currentTimeMillis();
		Logger.debug(clazz, "finish invoke %s at %s", method, end);
	}
}

 日志相关的处理,在方法层

 

Controller拦截处理,around:

 private enum ReturnType {
        JSON_OBJECT, JSON_RESULT, VOID, PATH, DIRECT_STRING,
    }

    @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public Object handlerRequestMapping(final ProceedingJoinPoint joinPoint) throws Throwable {
        final Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        ReturnType returnType = ReturnType.PATH;
        if (void.class.equals(method.getReturnType())) {// 返回值为void
            if (method.getAnnotation(ResponseResult.class) != null) {
                returnType = ReturnType.JSON_RESULT;
            } else {
                returnType = ReturnType.VOID;
            }
        } else if (String.class.equals(method.getReturnType())) {// 返回值为String
            if (method.getAnnotation(NotEscape.class) != null) {
                returnType = ReturnType.DIRECT_STRING;
            } else {
                returnType = ReturnType.PATH;
            }
        } else {
            returnType = ReturnType.JSON_OBJECT;
        }
        try {
            final Object result = joinPoint.proceed();
            if (result != null || returnType == ReturnType.JSON_RESULT) {
                switch (returnType) {
                case DIRECT_STRING:
                    writeDirectToResponse(ResponseHolder.get(), result);
                    break;
                case JSON_OBJECT:
                    writeJsonEscapeToResponse(ResponseHolder.get(), result);
                    break;
                case PATH:
                    // 指向JSP路径,直接返回
                    return result;
                case VOID:
                    // 方法中已经处理了返回,无需再做操作
                    break;
                case JSON_RESULT:
                    final AjaxResultBean ajaxResultBean = new AjaxResultBean();
                    ajaxResultBean.setSuccess(true);
                    ajaxResultBean.setMessage(ErrorUtil.getSuccessMessage());
                    ajaxResultBean.setKey(getErrorCode(((MethodSignature) joinPoint.getSignature()).getMethod(), ErrorCode.SUCCESS));
                    writeJsonEscapeToResponse(ResponseHolder.get(), ajaxResultBean);
                    break;
                default:
                    break;
                }
            }
        } catch (final Throwable e) {
            final AjaxResultBean ajaxResultBean = new AjaxResultBean();
            ajaxResultBean.setSuccess(false);
            if (YtoBusinessException.class.isAssignableFrom(e.getClass())) {
            	//TODO FIX
//                ajaxResultBean.setKey(getErrorCode(((MethodSignature) joinPoint.getSignature()).getMethod(), ((YtoBusinessException) e).getCode()));
                //TODO fix
//                ajaxResultBean.setMessage(getErrorMessage(((MethodSignature) joinPoint.getSignature()).getMethod(), ((YtoBusinessException) e).getCode()));
            } else {
                ajaxResultBean.setKey(getErrorCode(((MethodSignature) joinPoint.getSignature()).getMethod(), ErrorCode.DEFAULT_ERROR));
                ajaxResultBean.setMessage(getErrorMessage(((MethodSignature) joinPoint.getSignature()).getMethod(), ErrorCode.DEFAULT_ERROR));
            }
            switch (returnType) {
            case DIRECT_STRING:
                writeDirectToResponse(ResponseHolder.get(), ajaxResultBean.getMessage());
                break;
            case JSON_OBJECT:
                writeJsonEscapeToResponse(ResponseHolder.get(), ajaxResultBean);
                break;
            case PATH:
                // 指向JSP路径,直接返回
                RequestHolder.get().setAttribute("errorBean", ajaxResultBean);
                return "error.index";
            case VOID:
                // 方法中已经处理了返回,无需再做操作
                break;
            case JSON_RESULT:
                writeJsonEscapeToResponse(ResponseHolder.get(), ajaxResultBean);
                break;
            default:
                break;
            }
        }
        return null;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值