SpringBoot 基于@AspectJ的切面编程(AOP)

来源:互联网 发布:淘宝怎样撤销物流投诉 编辑:程序博客网 时间:2024/05/16 00:52

“Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. (Such concerns are often termed crosscutting concerns in AOP literature.)”

相关术语

Terms Description Aspect 切面,横切关注点的抽象。一个包含多个API的模块。 JoinPoint 连接点,AOP切面编程事件处理入口。 Advice 通知,方法执行前后需要实际执行的动作。 Pointcut 切入点,定义通知触发的规则。 Target object   代理的目标对象 Weaving 织入,将切面与其他对象融合并创建一个新的通知对象,可以在编译,加载或者运行时期。 Introduction 在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

通知类型

  • Before
  • After
  • Around
  • AfterReturing
  • AfterThrowing

声明一个自定义切面

@Component@Slf4j@Aspectpublic class ExecutionTimeLogger {    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")    public void requestMapping() {    }    @Pointcut("execution(* trivia.ui.controller.*.*(..))")    public void methodPointcut() {    }    @Around("requestMapping() && methodPointcut()")    public Object profile(ProceedingJoinPoint pjp) throws Throwable {        StopWatch sw = new StopWatch();        String name = pjp.getSignature().getName();        log.info("around start target = {}, args = {}", pjp.getArgs(), pjp.getTarget());        try {            sw.start();            return pjp.proceed();        } finally {            sw.stop();            log.info("around end: " + sw.getTime() + " - " + name);        }    }    @Before("requestMapping() && methodPointcut()")    public void before(JoinPoint jp) throws Throwable {        log.info("before method {}", jp.getSignature().getName());    }    @AfterReturning("requestMapping() && methodPointcut()")    public void afterReturning(JoinPoint jp) throws Throwable {        log.info("afterReturning method {}", jp.getSignature().getName());    }    @AfterThrowing("requestMapping() && methodPointcut()")    public void afterThrowing(JoinPoint joinPoint) throws Throwable {        log.info("afterThrowing method is {}",joinPoint.getSignature().getName());    }    @After("requestMapping() && methodPointcut()")    public void after(JoinPoint joinPoint) {        log.info("after method {}", joinPoint.getSignature().getName());     }}

启动SpringBootApplication,执行结果如下:

2017-11-30 20:25:52.963  INFO 12472 --- [nio-8080-exec-1] trivia.ui.aop.ExecutionTimeLogger        : around start target = [trivia.ui.dto.AuthenticationDTO@6ef6f07b], args = trivia.ui.controller.AuthenticationController@6710da862017-11-30 20:25:52.964  INFO 12472 --- [nio-8080-exec-1] trivia.ui.aop.ExecutionTimeLogger        : before method authenticate2017-11-30 20:25:53.159  INFO 12472 --- [nio-8080-exec-1] t.u.controller.AuthenticationController  : authenticate token is trivia.ui.dto.AuthTokenDTO@43a8d5902017-11-30 20:25:53.159  INFO 12472 --- [nio-8080-exec-1] trivia.ui.aop.ExecutionTimeLogger        : around end: 195 - authenticate2017-11-30 20:25:53.159  INFO 12472 --- [nio-8080-exec-1] trivia.ui.aop.ExecutionTimeLogger        : after method authenticate2017-11-30 20:25:53.159  INFO 12472 --- [nio-8080-exec-1] trivia.ui.aop.ExecutionTimeLogger        : afterReturning method authenticate

https://www.tutorialspoint.com/spring/aop_with_spring.htm

原创粉丝点击