1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| package com.sankuai.cx.quickpass.bankgate.biz;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component;
@Aspect @Component public class LogAspect { @Around(value = "execution(* test(..)) && @annotation(com.sankuai.cx.quickpass.bankgate.service.MockedService.Annotation1)") public Object execute(final ProceedingJoinPoint pjp) throws Throwable{ System.out.println("before aspect1"); Object o = pjp.proceed(); System.out.println("after aspect1"); return o; } } package com.sankuai.cx.quickpass.bankgate.biz;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component;
@Aspect @Component public class ConfigAspect { @Around(value = "execution(* test(..)) && @annotation(com.sankuai.cx.quickpass.bankgate.service.MockedService.Annotation2)") public Object execute(final ProceedingJoinPoint pjp) throws Throwable{ System.out.println("before aspect2"); Object o = pjp.proceed(); System.out.println("after aspect2"); return o; } } package com.sankuai.cx.quickpass.bankgate.biz;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component;
@Aspect @Component public class ExceptionAspect { @Around(value = "execution(* test(..)) && @annotation(com.sankuai.cx.quickpass.bankgate.service.MockedService.Annotation3)") public Object execute(final ProceedingJoinPoint pjp) throws Throwable{ System.out.println("before aspect3"); Object o = pjp.proceed(); System.out.println("after aspect3"); return o; } } package com.sankuai.cx.quickpass.bankgate.service.MockedService;
import groovy.transform.AnnotationCollector; import org.springframework.stereotype.Component;
@Component public class MyService {
@Annotation1 @Annotation2 @Annotation3 public Object test(){ System.out.println("before test"); String s = new String(); System.out.println("after test"); return s; } } package com.sankuai.cx.quickpass.bankgate.biz;
import com.sankuai.cx.quickpass.bankgate.service.MockedService.MyService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.Resource;
@WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class TestAspect { @Resource MyService myService;
@Test public void test(){ myService.test(); } }
|
执行结果
1 2 3 4 5 6 7 8
| before aspect2 before aspect3 before aspect1 before test after test after aspect1 after aspect3 after aspect2
|
从执行结果中可以看出来,AOP切面的执行过程是责任链模式的执行过程。
而各AOP的执行顺序,是按照AOP处理器按照className升序排列的顺序执行的
此例中的执行顺序就是:ConfigAspect---->ExceptionAspect---->LogAspect