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.)”
相关术语
通知类型
- 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
阅读全文
0 0
- SpringBoot 基于@AspectJ的切面编程(AOP)
- Spring面向切面编程--AOP,AspectJ,基于注解方式。
- Spring AOP切面编程 AspectJ
- Spring AOP编程(两种:1,传统的AOP切面编程 2,基于aspectj框架切点传统开发)
- 深入理解面向切面的编程AOP、AspectJ、Spring
- AspectJ教程--AOP面向切面编程框架(Android)
- SpringBoot 面向切面编程AOP
- SpringBoot:AOP Aspect切面编程
- spring基于AspectJ的AOP编程
- AOP-面向切面编程——AspectJ
- spring-aop---AspectJ注解式切面编程。
- spring实战-基于注解的面向切面编程(AOP)
- 基于@AspectJ的AOP
- 基于@AspectJ的AOP
- 面向切面编程(AOP)应用,日志切面,基于注解
- Spring-AOP切面+Aspectj框架的使用
- java spring aop 支持的AspectJ 切面
- Spring3.0 学习-AOP面向切面编程_Spring AOP的注解模式即Aspectj模式
- 傅里叶变换库fftw的安装配置以及可能的错误处理(VS2015)
- 百分比画圆
- eclipse 删除所有注释及空白行
- 前端面筋-客户端存储
- Java并发编程:volatile关键字解析
- SpringBoot 基于@AspectJ的切面编程(AOP)
- Uva 1595
- 图片应用与帧动画
- mysql配置允许外界连接
- 十一月英语博客
- Vue学习过程中的问题记录本(持续更新)
- android自定义view小圆球指哪跑哪
- 数据库连接池性能比对(hikari druid c3p0 dbcp jdbc)
- 51Nod 1094 和为k的连续区间 map