【資料圖】
專題使用匯總:
8. Java-AOP
1.AOP:面向切面編程,就是面向特定方法編程。在不改變原有方法的基礎上新增功能(功能增強,功能改變)2.SpringAOP3.SpringAOP 開發(fā)步驟;
一.使用場景:1.記錄操作日志2.權限控制3.事務管理4.記錄方法執(zhí)行時間
二.優(yōu)勢1.代碼無浸入2.減少重復代碼3.提高開發(fā)效率4.維護方便
三.操作:執(zhí)行流程,動態(tài)代理技術,生成動態(tài)代理對象(實現(xiàn)功能增強)3.1 添加依賴在pom.xml
org.springframework.boot spring-boot-starter-aop
3.2 編寫AOP程序:針對特定方法業(yè)務需要進行編程
@Slf4j@Component//@Aspect //AOP類public class TimeAspect { //@Around("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))") //切入點表達式,對所有類,接口記錄執(zhí)行時間 @Around("com.itheima.aop.MyAspect1.pt()") public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable { //1. 記錄開始時間 long begin = System.currentTimeMillis(); //2. 調用原始方法運行 Object result = joinPoint.proceed(); //3. 記錄結束時間, 計算方法執(zhí)行耗時 long end = System.currentTimeMillis(); log.info(joinPoint.getSignature()+"方法執(zhí)行耗時: {}ms", end-begin); return result; }}
四.核心概念1.連接點: JoinPoint 可以被AOP控制的方法2.通知:Advice 指那些重復的邏輯,即共性的功能3.切入點:PointCut 匹配連接點的條件
@Pointcut("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))") public void pt(){} @Pointcut("execution(* com.itheima.service.DeptService.list()) || " + "execution(* com.itheima.service.DeptService.delete(java.lang.Integer))") private void pt(){}
[責任編輯:linlin]
標簽: