配置切入点表达式|| 前置通知、后置通知、异常通知、最终通知、环绕通知
生活随笔
收集整理的這篇文章主要介紹了
配置切入点表达式|| 前置通知、后置通知、异常通知、最终通知、环绕通知
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?環(huán)繞通知
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.dym</groupId><artifactId>day03_eesy_04adviceType</artifactId><version>1.0-SNAPSHOT</version><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.6</source><target>1.6</target></configuration></plugin></plugins></build><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.7</version></dependency></dependencies></project>IAccountService.java
package com.itheima.service;/*** 賬戶的業(yè)務層接口*/ public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount();/*** 模擬更新賬戶* @param i*/void updateAccount(int i);/*** 刪除賬戶* @return*/int deleteAccount(); }AccountServiceImpl.java
package com.itheima.service.impl;import com.itheima.service.IAccountService;/*** 賬戶的業(yè)務層實現類*/ public class AccountServiceImpl implements IAccountService{@Overridepublic void saveAccount() {System.out.println("執(zhí)行了保存"); // int i=1/0;}@Overridepublic void updateAccount(int i) {System.out.println("執(zhí)行了更新"+i);}@Overridepublic int deleteAccount() {System.out.println("執(zhí)行了刪除");return 0;} }Logger.java
package com.itheima.utils;import org.aspectj.lang.ProceedingJoinPoint;/*** 用于記錄日志的工具類,它里面提供了公共的代碼*/ public class Logger {/*** 前置通知*/public void beforePrintLog(){System.out.println("前置通知Logger類中的beforePrintLog方法開始記錄日志了。。。");}/*** 后置通知*/public void afterReturningPrintLog(){System.out.println("后置通知Logger類中的afterReturningPrintLog方法開始記錄日志了。。。");}/*** 異常通知*/public void afterThrowingPrintLog(){System.out.println("異常通知Logger類中的afterThrowingPrintLog方法開始記錄日志了。。。");}/*** 最終通知*/public void afterPrintLog(){System.out.println("最終通知Logger類中的afterPrintLog方法開始記錄日志了。。。");}public Object aroundPringLog(ProceedingJoinPoint pjp){Object rtValue = null;try{Object[] args = pjp.getArgs();//得到方法執(zhí)行所需的參數System.out.println("Logger類中的aroundPringLog方法開始記錄日志了。。。前置");rtValue = pjp.proceed(args);//明確調用業(yè)務層方法(切入點方法)System.out.println("Logger類中的aroundPringLog方法開始記錄日志了。。。后置");return rtValue;}catch (Throwable t){System.out.println("Logger類中的aroundPringLog方法開始記錄日志了。。。異常");throw new RuntimeException(t);}finally {System.out.println("Logger類中的aroundPringLog方法開始記錄日志了。。。最終");}} }bean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置srping的Ioc,把service對象配置進來--><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean><!-- 配置Logger類 --><bean id="logger" class="com.itheima.utils.Logger"></bean><!--配置AOP--><aop:config><aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut><!--配置切面 --><aop:aspect id="logAdvice" ref="logger"><!--配置前置通知:在切入點方法執(zhí)行之前執(zhí)行--><!--<aop:before method="beforePrintLog" pointcut-ref="pt1" ></aop:before>--><!--<!– 配置后置通知:在切入點方法正常執(zhí)行之后值。它和異常通知永遠只能執(zhí)行一個--><!--<aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>--><!--配置異常通知:在切入點方法執(zhí)行產生異常之后執(zhí)行。它和后置通知永遠只能執(zhí)行一個--><!--<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>--><!--配置最終通知:無論切入點方法是否正常執(zhí)行它都會在其后面執(zhí)行--><!--<aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>--><!-- 配置環(huán)繞通知 詳細的注釋請看Logger類中--><aop:around method="aroundPringLog" pointcut-ref="pt1"></aop:around></aop:aspect></aop:config></beans>總結
以上是生活随笔為你收集整理的配置切入点表达式|| 前置通知、后置通知、异常通知、最终通知、环绕通知的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring中基于XML的AOP配置步骤
- 下一篇: Spring基于注解的AOP配置