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;
/**
* @Author:wangchao
* @Date: 2018/8/1 下午5:48
* @Description:
**/
public class LogAspect {
"execution(* test(..)) && @annotation(com.sankuai.cx.quickpass.bankgate.service.MockedService.Annotation1)") (value =
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;
/**
* @Author:wangchao
* @Date: 2018/8/1 下午5:48
* @Description:
**/
public class ConfigAspect {
"execution(* test(..)) && @annotation(com.sankuai.cx.quickpass.bankgate.service.MockedService.Annotation2)") (value =
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;
/**
* @Author:wangchao
* @Date: 2018/8/1 下午5:48
* @Description:
**/
public class ExceptionAspect {
"execution(* test(..)) && @annotation(com.sankuai.cx.quickpass.bankgate.service.MockedService.Annotation3)") (value =
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;
/**
* @Author:wangchao
* @Date: 2018/8/1 下午5:55
* @Description:
**/
public class MyService {
1
2
3
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;
/**
* @Author:wangchao
* @Date: 2018/8/1 下午5:51
* @Description:
**/
(SpringJUnit4ClassRunner.class)
"classpath:applicationContext.xml") (locations =
public class TestAspect {
MyService myService;
public void test(){
myService.test();
}
}
执行结果
before aspect2
before aspect3
before aspect1
before test
after test
after aspect1
after aspect3
after aspect2
从执行结果中可以看出来,AOP切面的执行过程是责任链模式的执行过程。
而各AOP的执行顺序,是按照AOP处理器按照className升序排列的顺序执行的
此例中的执行顺序就是:ConfigAspect---->ExceptionAspect---->LogAspect