五、AOP入門
阿新 • • 發佈:2020-11-19
AOP:【動態代理】
指在程式執行期間動態的將某段程式碼切入到指定方法指定位置進行執行的程式設計方式;
一、匯入aop模組;Spring AOP:(spring-aspects)
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.12.RELEASE</version> </dependency>
二、定義一個業務邏輯類(MathCalculator)
public class MathCalculator { public int div(int i,int j){ System.out.println("MathCalculator...div..."); return i/j; } }
- 給切面類的目標方法(div方法)標註何時何地執行(通知註解);
- 將切面類和業務邏輯類(目標方法所在類)都加入到容器中;
- 必須告訴Spring哪個類是切面類(給切面類上加一個註解:@Aspect)
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
@Aspect public class LogAspects { //抽取公共的切入點表示式 //1、本類引用 //2、其他的切面引用 @Pointcut("execution(public int com.atguigu.aop.MathCalculator.*(..))") public void pointCut(){}; //@Before在目標方法之前切入;切入點表示式(指定在哪個方法切入) @Before("pointCut()") public void logStart(JoinPoint joinPoint){ Object[] args = joinPoint.getArgs(); System.out.println(""+joinPoint.getSignature().getName()+"執行。。。@Before:引數列表是:{"+Arrays.asList(args)+"}"); } @After("com.atguigu.aop.LogAspects.pointCut()") public void logEnd(JoinPoint joinPoint){ System.out.println(""+joinPoint.getSignature().getName()+"結束。。。@After"); } //JoinPoint一定要出現在引數表的第一位 @AfterReturning(value="pointCut()",returning="result") public void logReturn(JoinPoint joinPoint,Object result){ System.out.println(""+joinPoint.getSignature().getName()+"正常返回。。。@AfterReturning:執行結果:{"+result+"}"); } @AfterThrowing(value="pointCut()",throwing="exception") public void logException(JoinPoint joinPoint,Exception exception){ System.out.println(""+joinPoint.getSignature().getName()+"異常。。。異常資訊:{"+exception+"}"); }
@Test public void test01(){ AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAOP.class); //1、不要自己建立物件 // MathCalculator mathCalculator = new MathCalculator(); // mathCalculator.div(1, 1); MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class); mathCalculator.div(1, 0); applicationContext.close(); }
將業務邏輯元件和切面類都加入到容器中;告訴Spring哪個是切面類(@Aspect)
在切面類上的每一個通知方法上標註通知註解,告訴Spring何時何地執行(切入點表示式)