AOP-同一方法配置多个AOP切面

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:
**/
@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;

/**
* @Author:wangchao
* @Date: 2018/8/1 下午5:48
* @Description:
**/
@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;

/**
* @Author:wangchao
* @Date: 2018/8/1 下午5:48
* @Description:
**/
@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;

/**
* @Author:wangchao
* @Date: 2018/8/1 下午5:55
* @Description:
**/
@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;

/**
* @Author:wangchao
* @Date: 2018/8/1 下午5:51
* @Description:
**/
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class TestAspect {
@Resource
MyService myService;

@Test
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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值