| | |
| | | package com.yeshi.fanli.aspect;
|
| | |
|
| | | import java.io.PrintWriter;
|
| | |
|
| | | 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.Before;
|
| | |
|
| | | //@Component
|
| | | //@Aspect
|
| | | public class TestAspect {
|
| | | public static final String EDP = "execution(* com.yeshi.shop.controller.**.*(..))";
|
| | |
|
| | | @Before(EDP)
|
| | | public void testBefore(JoinPoint joinpoint) {
|
| | | System.out.println("testBefore");
|
| | | System.out.println(joinpoint.getArgs().length);
|
| | | }
|
| | |
|
| | | @After(EDP)
|
| | | public void testAfter(JoinPoint joinpoint) {
|
| | | System.out.println("testAfter");
|
| | | Object[] objs = joinpoint.getArgs();
|
| | | }
|
| | |
|
| | | @AfterReturning(pointcut = EDP, returning = "rvt")
|
| | | public void testAfterReturning(JoinPoint joinpoint, Object rvt) {
|
| | | System.out.println("testAfterReturning");
|
| | | }
|
| | |
|
| | | @AfterThrowing(throwing = "e", pointcut = EDP)
|
| | | public void testAfterThrowing(JoinPoint joinpoint, Exception e) {
|
| | | System.out.println("testAfterThrowing");
|
| | | }
|
| | |
|
| | | @Around(EDP)
|
| | | public Object testAround(ProceedingJoinPoint joinPoint) {
|
| | | |
| | | Object[] args = joinPoint.getArgs();
|
| | | PrintWriter print = (PrintWriter) args[1];
|
| | | print.print("AOP测试。");
|
| | | Object obj = null;
|
| | | try {
|
| | | // obj = joinPoint.proceed(args);
|
| | | } catch (Throwable e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return obj;
|
| | | }
|
| | |
|
| | | }
|
| | | package com.yeshi.fanli.aspect; |
| | | |
| | | import java.io.PrintWriter; |
| | | |
| | | 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.Before; |
| | | |
| | | //@Component |
| | | //@Aspect |
| | | public class TestAspect { |
| | | public static final String EDP = "execution(* com.yeshi.shop.controller.**.*(..))"; |
| | | |
| | | @Before(EDP) |
| | | public void testBefore(JoinPoint joinpoint) { |
| | | System.out.println("testBefore"); |
| | | System.out.println(joinpoint.getArgs().length); |
| | | } |
| | | |
| | | @After(EDP) |
| | | public void testAfter(JoinPoint joinpoint) { |
| | | System.out.println("testAfter"); |
| | | Object[] objs = joinpoint.getArgs(); |
| | | } |
| | | |
| | | @AfterReturning(pointcut = EDP, returning = "rvt") |
| | | public void testAfterReturning(JoinPoint joinpoint, Object rvt) { |
| | | System.out.println("testAfterReturning"); |
| | | } |
| | | |
| | | @AfterThrowing(throwing = "e", pointcut = EDP) |
| | | public void testAfterThrowing(JoinPoint joinpoint, Exception e) { |
| | | System.out.println("testAfterThrowing"); |
| | | } |
| | | |
| | | @Around(EDP) |
| | | public Object testAround(ProceedingJoinPoint joinPoint) { |
| | | |
| | | Object[] args = joinPoint.getArgs(); |
| | | PrintWriter print = (PrintWriter) args[1]; |
| | | print.print("AOP测试。"); |
| | | Object obj = null; |
| | | try { |
| | | // obj = joinPoint.proceed(args); |
| | | } catch (Throwable e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return obj; |
| | | } |
| | | |
| | | } |