AOP:Aspect Oriented Programming(面向切面编程、面向方面编程)
- 减少重复代码:不需要在业务方法中定义大量的重复性的代码,只需要将重复性的代码抽取到AOP程序中即可。
- 代码无侵入:在基于AOP实现这些业务功能时,对原有的业务代码是没有任何侵入的,不需要修改任何的业务代码。
- 提高开发效率
- 维护方便
1. 导入AOP依赖及入门代码实现
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
|
切面类用来记录方法执行时间的例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @Component @Aspect @Slf4j public class RecordTimeAspect {
@Around("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))") public Object recordTime(ProceedingJoinPoint pjp) throws Throwable { long begin = System.currentTimeMillis();
Object result = pjp.proceed();
long end = System.currentTimeMillis();
log.info("方法执行耗时: {}毫秒",end-begin); return result; } }
|