2026/4/15 17:26:45
网站建设
项目流程
酒店网站案例,1688网站怎样做推广,企业登记代理,高端建筑围护系统通知类型
通知类型包括#xff1a;
前置通知#xff1a;Before 目标方法执行之前的通知后置通知#xff1a;AfterReturning 目标方法执行之后的通知环绕通知#xff1a;Around 目标方法之前添加通知#xff0c;同时目标方法执行之后添加通知。异常通知#xff1a;AfterTh…通知类型通知类型包括前置通知Before 目标方法执行之前的通知后置通知AfterReturning 目标方法执行之后的通知环绕通知Around 目标方法之前添加通知同时目标方法执行之后添加通知。异常通知AfterThrowing 发生异常之后执行的通知最终通知After 放在finally语句块中的通知接下来编写程序来测试这几个通知的执行顺序packagecom.powernode.spring6.service;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.springframework.stereotype.Component;// 切面类ComponentAspectpublicclassMyAspect{Around(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidaroundAdvice(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{System.out.println(环绕通知开始);// 执行目标方法。proceedingJoinPoint.proceed();System.out.println(环绕通知结束);}Before(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidbeforeAdvice(){System.out.println(前置通知);}AfterReturning(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidafterReturningAdvice(){System.out.println(后置通知);}AfterThrowing(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidafterThrowingAdvice(){System.out.println(异常通知);}After(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidafterAdvice(){System.out.println(最终通知);}}packagecom.powernode.spring6.service;importorg.springframework.stereotype.Component;// 目标类ComponentpublicclassOrderService{// 目标方法publicvoidgenerate(){System.out.println(订单已生成);}}packagecom.powernode.spring6.test;importcom.powernode.spring6.service.OrderService;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassAOPTest{TestpublicvoidtestAOP(){ApplicationContextapplicationContextnewClassPathXmlApplicationContext(spring-aspectj-aop-annotation.xml);OrderServiceorderServiceapplicationContext.getBean(orderService,OrderService.class);orderService.generate();}}执行结果通过上面的执行结果就可以判断他们的执行顺序了这里不再赘述。结果中没有异常通知这是因为目标程序执行过程中没有发生异常。我们尝试让目标方法发生异常packagecom.powernode.spring6.service;importorg.springframework.stereotype.Component;// 目标类ComponentpublicclassOrderService{// 目标方法publicvoidgenerate(){System.out.println(订单已生成);if(11){thrownewRuntimeException(模拟异常发生);}}}再次执行测试程序结果如下通过测试得知当发生异常之后最终通知也会执行因为最终通知After会出现在finally语句块中。出现异常之后后置通知和环绕通知的结束部分不会执行。切面的先后顺序我们知道业务流程当中不一定只有一个切面可能有的切面控制事务有的记录日志有的进行安全控制如果多个切面的话顺序如何控制可以使用Order注解来标识切面类为Order注解的value指定一个整数型的数字数字越小优先级越高。再定义一个切面类如下packagecom.powernode.spring6.service;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.springframework.core.annotation.Order;importorg.springframework.stereotype.Component;AspectComponentOrder(1)//设置优先级publicclassYourAspect{Around(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidaroundAdvice(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{System.out.println(YourAspect环绕通知开始);// 执行目标方法。proceedingJoinPoint.proceed();System.out.println(YourAspect环绕通知结束);}Before(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidbeforeAdvice(){System.out.println(YourAspect前置通知);}AfterReturning(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidafterReturningAdvice(){System.out.println(YourAspect后置通知);}AfterThrowing(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidafterThrowingAdvice(){System.out.println(YourAspect异常通知);}After(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidafterAdvice(){System.out.println(YourAspect最终通知);}}packagecom.powernode.spring6.service;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.springframework.core.annotation.Order;importorg.springframework.stereotype.Component;// 切面类ComponentAspectOrder(2)//设置优先级publicclassMyAspect{Around(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidaroundAdvice(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{System.out.println(环绕通知开始);// 执行目标方法。proceedingJoinPoint.proceed();System.out.println(环绕通知结束);}Before(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidbeforeAdvice(){System.out.println(前置通知);}AfterReturning(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidafterReturningAdvice(){System.out.println(后置通知);}AfterThrowing(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidafterThrowingAdvice(){System.out.println(异常通知);}After(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidafterAdvice(){System.out.println(最终通知);}}执行测试程序通过修改Order注解的整数值来切换顺序执行测试程序优化使用切点表达式观看以下代码中的切点表达式packagecom.powernode.spring6.service;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.springframework.core.annotation.Order;importorg.springframework.stereotype.Component;// 切面类ComponentAspectOrder(2)publicclassMyAspect{Around(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidaroundAdvice(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{System.out.println(环绕通知开始);// 执行目标方法。proceedingJoinPoint.proceed();System.out.println(环绕通知结束);}Before(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidbeforeAdvice(){System.out.println(前置通知);}AfterReturning(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidafterReturningAdvice(){System.out.println(后置通知);}AfterThrowing(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidafterThrowingAdvice(){System.out.println(异常通知);}After(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidafterAdvice(){System.out.println(最终通知);}}缺点是第一切点表达式重复写了多次没有得到复用。第二如果要修改切点表达式需要修改多处难维护。可以这样做将切点表达式单独的定义出来在需要的位置引入即可。如下packagecom.powernode.spring6.service;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.springframework.core.annotation.Order;importorg.springframework.stereotype.Component;// 切面类ComponentAspectOrder(2)publicclassMyAspect{Pointcut(execution(* com.powernode.spring6.service.OrderService.*(..)))publicvoidpointcut(){}Around(pointcut())publicvoidaroundAdvice(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{System.out.println(环绕通知开始);// 执行目标方法。proceedingJoinPoint.proceed();System.out.println(环绕通知结束);}Before(pointcut())publicvoidbeforeAdvice(){System.out.println(前置通知);}AfterReturning(pointcut())publicvoidafterReturningAdvice(){System.out.println(后置通知);}AfterThrowing(pointcut())publicvoidafterThrowingAdvice(){System.out.println(异常通知);}After(pointcut())publicvoidafterAdvice(){System.out.println(最终通知);}}使用Pointcut注解来定义独立的切点表达式。注意这个Pointcut注解标注的方法随意只是起到一个能够让Pointcut注解编写的位置。执行测试程序