声明通知Advice
- 配置方式(以前置通知为例子)
- 方式一
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut> <aop:before method="aspectBefore" pointcut-ref="ikPoint"></aop:before> </aop:aspect></aop:config>
- 优点:前置通知、后置通知、环绕通知使用同一个切点时,配置一个<aop:poincut />即可,方便配置。
- 方式二
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:before method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before> </aop:aspect> </aop:config>
- 优点:前置通知、后置通知、环绕通知使用不同切点时,不需要配置<aop:poincut />元素,可以直接在<aop:before />元素内配置切点。
- Next
- 方式一
- 前置通知(Before Advice)
- 在切入点时机事务执行之前,执行通知
- 方式一
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut> <aop:before method="aspectBefore" pointcut-ref="ikPoint"></aop:before> </aop:aspect> </aop:config>
- 方式二
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:before method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before> </aop:aspect> </aop:config>
- Next
- 后置通知(After Running Advice)
- 在切入点时机事务执行之后,执行通知(前提:切入点执行时没有异常,否则不会执行)
- 方式一
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut> <aop:after-returning method="aspectBefore" pointcut-ref="ikPoint"></aop:before> </aop:aspect> </aop:config>
- 方式二
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:after-returning method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before> </aop:aspect> </aop:config>
- Next
- 抛出异常通知(After Throwing Advice)
- 若在切入点时机执行时抛出异常,执行通知。(执行异常通知,则不会执行后置通知)
- 方式一
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut> <aop:after-throwing method="aspectBefore" pointcut-ref="ikPoint"></aop:before> </aop:aspect> </aop:config>
- 方式二
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:after-throwing method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before> </aop:aspect> </aop:config>
- Next
- 后通知(After Ending Advice)
- 在切入点时机执行之后,执行通知。(切入点执行时,无论正常执行,还是抛出异常,都会执行通知,相当于try_catch_finally)。
- 方式一
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut> <aop:after method="aspectBefore" pointcut-ref="ikPoint"></aop:before> </aop:aspect> </aop:config>
- 方式二
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:after method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before> </aop:aspect> </aop:config>
- Next
- 无论切面是否出现异常,后通知动作正常执行
- 环绕通知(Around Advice)
- 环绕通知在
- 方式一
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" > <bean id="ikAspect" class="com.jing.spring.aop.IKAspect"></bean> <bean id="ikAspectBiz" class="com.jing.spring.aop.IKAspectBiz"></bean> <aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut> <aop:around method="aspectAround" pointcut-ref="ikPoint"></aop:around> </aop:aspect> </aop:config> </beans>
package com.jing.spring.aop; import org.aspectj.lang.ProceedingJoinPoint; public class IKAspect { public void aspectAround(ProceedingJoinPoint pj){ System.out.println("IKAspect.aspectAroud,its 环绕通知前"); try { Object proceed = pj.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println("IKAspect.aspectAroud,its 环绕通知后"); } }
- 方式二
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" > <bean id="ikAspect" class="com.jing.spring.aop.IKAspect"></bean> <bean id="ikAspectBiz" class="com.jing.spring.aop.IKAspectBiz"></bean> <aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:around method="aspectAround" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:around> </aop:aspect> </aop:config> </beans>
package com.jing.spring.aop; import org.aspectj.lang.ProceedingJoinPoint; public class IKAspect { public void aspectAround(ProceedingJoinPoint pj){ System.out.println("IKAspect.aspectAroud,its 环绕通知前"); try { Object proceed = pj.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println("IKAspect.aspectAroud,its 环绕通知后"); } }
- Next
原文地址:https://www.cnblogs.com/jixue/p/10432032.html
时间: 2024-10-12 10:13:29