|
| 1 | +package cn.com.ttblog.ssmbootstrap_table.aop; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.builder.ToStringBuilder; |
| 4 | +import org.aspectj.lang.JoinPoint; |
| 5 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 6 | +import org.aspectj.lang.annotation.After; |
| 7 | +import org.aspectj.lang.annotation.AfterReturning; |
| 8 | +import org.aspectj.lang.annotation.AfterThrowing; |
| 9 | +import org.aspectj.lang.annotation.Around; |
| 10 | +import org.aspectj.lang.annotation.Aspect; |
| 11 | +import org.aspectj.lang.annotation.Before; |
| 12 | +import org.aspectj.lang.annotation.Pointcut; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | +import org.springframework.stereotype.Component; |
| 16 | + |
| 17 | +@Component |
| 18 | +@Aspect |
| 19 | +public class ControllerAspect { |
| 20 | + |
| 21 | + private static final Logger LOG = LoggerFactory |
| 22 | + .getLogger(ControllerAspect.class); |
| 23 | + |
| 24 | + @Pointcut("execution(* cn.com.ttblog.ssmbootstrap_table.controller..*.*(..))") |
| 25 | + public void pointCut() { |
| 26 | + } |
| 27 | + |
| 28 | + @After("pointCut()") |
| 29 | + public void after(JoinPoint joinPoint) { |
| 30 | + } |
| 31 | + |
| 32 | + @Before("pointCut()") |
| 33 | + public void before(JoinPoint joinPoint) { |
| 34 | + // Object[] args = joinPoint.getArgs(); |
| 35 | + LOG.warn("控制器切面参数 param:{}", |
| 36 | + ToStringBuilder.reflectionToString(joinPoint)); |
| 37 | + Object[] arr = joinPoint.getArgs(); |
| 38 | + if (arr.length > 0) { |
| 39 | + LOG.warn("method param:{}", |
| 40 | + ToStringBuilder.reflectionToString(joinPoint.getArgs()[0])); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @AfterReturning(pointcut = "pointCut()", returning = "returnVal") |
| 45 | + public void afterReturning(JoinPoint joinPoint, Object returnVal) { |
| 46 | + } |
| 47 | + |
| 48 | + @Around("pointCut()") |
| 49 | + public Object around(ProceedingJoinPoint pjp) throws Throwable { |
| 50 | + Object obj = null; |
| 51 | + long startTime = System.nanoTime(); |
| 52 | + long endTime; |
| 53 | + try { |
| 54 | + obj = pjp.proceed(); |
| 55 | + endTime = System.nanoTime(); |
| 56 | + } catch (Throwable ex) { |
| 57 | + throw ex; |
| 58 | + } |
| 59 | + LOG.warn(pjp.getSignature() + "-time consume is " |
| 60 | + + (endTime - startTime) / 1000 / 1000 / 1000 * 0.1 + "s"); |
| 61 | + return obj; |
| 62 | + } |
| 63 | + |
| 64 | + @AfterThrowing(pointcut = "pointCut()", throwing = "error") |
| 65 | + public void afterThrowing(JoinPoint jp, Throwable error) { |
| 66 | + LOG.error("控制器发生错误:" + error); |
| 67 | + } |
| 68 | +} |
0 commit comments