一、基于xml的AOP配置
aop:config:用于声明开始aop的配置
aop:aspect:配置切面
aop:pointcut:配置切入点表达式
aop:before:用于配置前置通知
aop:after-returning:用于配置后置通知
beans.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"><!--配置一个目标类的实例--><bean id="aopMethod" class="com.jy.aop.method.AopMethod"></bean><!--配置通知类的实例--><bean id="aopLogger" class="com.jy.aop.logger.AopLogger"></bean><!--开始配置aop--><aop:config><!--配置切面:要去引入通知类--><aop:aspect id="aspect" ref="aopLogger"><!--开始配置通知:pointcut:配置切入点表达式,就是设置通知的作用范围--><!--开始配置前置通知--><aop:before method="beforeAdvice" pointcut="execution(* com.jy.aop.method.AopMethod.*(..))"></aop:before><!--配置后置通知--><aop:after-returning method="afterAdvice" pointcut="execution(* com.jy.aop.method.AopMethod.*(..))"></aop:after-returning><!--配置异常通知--><aop:after-throwing method="exceptionAdvice" pointcut="execution(* com.jy.aop.method.AopMethod.*(..))"></aop:after-throwing><!--配置最终通知--><aop:after method="endAdvice" pointcut="execution(* com.jy.aop.method.AopMethod.*(..))"></aop:after></aop:aspect></aop:config></beans>
或者:
<?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"><!--配置一个目标类的实例--><bean id="aopMethod" class="com.jy.aop.method.AopMethod"></bean><!--配置通知类的实例--><bean id="aopLogger" class="com.jy.aop.logger.AopLogger"></bean><!--开始配置aop--><aop:config><!--配置切面:要去引入通知类--><aop:aspect id="aspect" ref="aopLogger"><!--声明一个切入点--><aop:pointcut id="pt01" expression="execution(* com.jy.aop.method.AopMethod.*(..))"/><!--开始配置通知:pointcut:配置切入点表达式,就是设置通知的作用范围--><!--开始配置前置通知--><aop:before method="beforeAdvice" pointcut-ref="pt01"></aop:before><!--配置后置通知--><aop:after-returning method="afterAdvice" pointcut-ref="pt01"></aop:after-returning><!--配置异常通知--><aop:after-throwing method="exceptionAdvice" pointcut-ref="pt01"></aop:after-throwing><!--配置最终通知--><aop:after method="endAdvice" pointcut-ref="pt01"></aop:after></aop:aspect></aop:config></beans>
二、基于注解配置aop
@Aspect:声明为切面类
@Before:前置通知的注解
@After:最终通知的注解
@AfterReturning:后置通知的注解
@AfterThrowing:异常通知的注解
@Around:环绕通知的注解
@Pointcut:切入点的注解
<?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"xmlns:context="http://www.springframework.org/schema/context"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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--开启spring扫描包--><context:component-scan base-package="com.jy"></context:component-scan><!--开启AOP注解的支持--><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
/*** @author shizi 2022/2/3* 日志管理类(通知类)*/@Component@Aspectpublic class AopLogger {/*** 声明一个切入点表达式*/@Pointcut("execution(* com.jy.aop.method.AopMethod.*(..))")public void pt01(){}/*** 前置通知*/@Before("pt01()")public void beforeAdvice(){System.out.println("前置通知开始记录日志了==");}/*** 后置通知*/@AfterReturning("pt01()")public void afterAdvice(){System.out.println("后置通知开始记录日志了==");}/*** 异常通知*/@AfterThrowing("pt01()")public void exceptionAdvice(){System.out.println("异常通知开始记录日志了");}/*** 最终通知*/@After("pt01()")public void endAdvice(){System.out.println("最终通知开始记录日志了");}}
三、环绕通知
环绕通知是invoke方法的整个过程,所以包含了其他通知,因此基于xml配置时,只需配置环绕通知。
基于注解的环绕通知很简单,只需在对应方法加上@Around注解.
可以获取目标方法的 完全控制权!(方法是否执行、控制参数、控制返回值)
在使用环绕通知时,目标方法的一切信息,都可以通过invocation(invoke方法传进去的参数名称)参数获取到
<?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"><!--配置一个目标类的实例--><bean id="aopMethod" class="com.jy.aop.method.AopMethod"></bean><!--配置通知类的实例--><bean id="aopLogger" class="com.jy.aop.logger.AopLogger"></bean><!--开始配置aop--><aop:config><!--配置切面:要去引入通知类--><aop:aspect id="aspect" ref="aopLogger"><!--声明一个切入点--><aop:pointcut id="pt01" expression="execution(* com.jy.aop.method.AopMethod.*(..))"/><!--开始配置通知:pointcut:配置切入点表达式,就是设置通知的作用范围--><aop:around method="aroundAdvice" pointcut-ref="pt01"></aop:around></aop:aspect></aop:config></beans>
/*** 环绕通知* @param proceedingJoinPoint*/public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint){//获取调用目标方法时的参数Object[] args = proceedingJoinPoint.getArgs();try {System.out.println("前置通知开始记录日志了==");//调用目标方法proceedingJoinPoint.proceed(args);System.out.println("后置通知开始记录日志了==");} catch (Throwable throwable) {System.out.println("异常通知开始记录日志了");throwable.printStackTrace();}finally {System.out.println("最终通知开始记录日志了");}}
