Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package com.job.lr.aop; import java.util.Arrays; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.stereotype.Component; /** * ? * * ? * * @author liuy * */ @Aspect @Component //@EnableAspectJAutoProxy public class LogAspect { /** * ?? * * ?com.fycoder.ll.web????? */ @Pointcut("execution(* com.job.lr.web..*(..))") //@Pointcut("execution(public * * (..))") public void pointcutExpression() { } /** * 1 ? * @param joinPoint */ @Before("pointcutExpression()") public void beforeMethod(JoinPoint joinPoint) { System.out.println("?"); } /** * 2 ? */ @After("pointcutExpression()") // ??. ? public void afterMethod(JoinPoint joinPoint) { System.out.println("?"); } /** * 3 * * ??? * ?! * * @param joinPoint * @param returnValue * */ @AfterReturning(value = "pointcutExpression()", returning = "returnValue") public void afterRunningMethod(JoinPoint joinPoint, Object returnValue) { System.out.println("" + returnValue); } /** * 4 * * ?. * ?; ?? * * @param joinPoint * @param e */ @AfterThrowing(value = "pointcutExpression()", throwing = "e") public void afterThrowingMethod(JoinPoint joinPoint, Exception e) { System.out.println(", " + e); } /** * ?? ProceedingJoinPoint ?. * ??: ProceedingJoinPoint ???. * , ? */ @Around("pointcutExpression()") public Object aroundMethod(ProceedingJoinPoint pjd) { Object result = null; String methodName = pjd.getSignature().getName(); try { //? System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs())); // result = pjd.proceed(); // System.out.println("The method " + methodName + " ends with " + result); } catch (Throwable e) { // System.out.println("The method " + methodName + " occurs exception:" + e); throw new RuntimeException(e); } //? System.out.println("The method " + methodName + " ends"); return result; } }