聲明通知Advice
- 配置方式(以前置通知為例子)
- 方式一
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:pointcut>
<aop:before method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
</aop:aspect>
</aop:config>package com.jing.spring.aop; /**
* 業務代碼
*/ public class IKAspectBiz { public void add(String name){ System.out.println("IKAspectBiz.add"); } }package com.jing.spring.aop; import org.aspectj.lang.ProceedingJoinPoint; /** * 切面代碼 */ public class IKAspect { public void aspectBefore(String name){ System.out.println("IKAspect.aspectBefore,it~s 前置通知"); System.out.println("IKAspect.aspectBefore,it~s 前置通知"+name); }
}- 優點:前置通知、后置通知、環繞通知使用同一個切點時,配置一個<aop:poincut />即可,方便配置。
- 方式二
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:before method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:before> </aop:aspect> </aop:config>
package com.jing.spring.aop; /**
* 業務代碼
*/ public class IKAspectBiz { public void add(String name){ System.out.println("IKAspectBiz.add"); } }package com.jing.spring.aop; import org.aspectj.lang.ProceedingJoinPoint; /** * 切面代碼 */ public class IKAspect { public void aspectBefore(String name){ System.out.println("IKAspect.aspectBefore,it~s 前置通知"); System.out.println("IKAspect.aspectBefore,it~s 前置通知"+name); } }
- 優點:前置通知、后置通知、環繞通知使用不同切點時,不需要配置<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.*(..)) and args(name)"></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.*(..)) and args(name)"></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.*(..)) and args(name)"></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.*(..)) and args(name)"></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.*(..)) and args(name) "></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.*(..)) and args(name)"></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.*(..)) and args(name)"></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.*(..)) and args(name)"></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.aopParams(String,int)) and args(name,times)"></aop:pointcut>
<aop:around method="aspectAroundParams" pointcut-ref="ikPoint"></aop:around>
</aop:aspect>
</aop:config>
</beans>package com.jing.spring.aop;
/**
* 業務代碼
*/
public class IKAspectBiz {
public void aopParams(String name,int times){
System.out.println("IKAspectBiz.aopParams");
}
}package com.jing.spring.aop; import org.aspectj.lang.ProceedingJoinPoint; /** * 切面代碼 */ public class IKAspect { public void aspectAroundParams(ProceedingJoinPoint pj,String name,int times){ System.out.println("IKAspect.aspectAroud,its 環繞通知前:"); System.out.println("IKAspect.aspectAroud,"+name+"-----"+times); try { Object proceed = pj.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println("IKAspect.aspectAroud,its 環繞通知后"); System.out.println("IKAspect.aspectAroud,"+name+"-----"+times); } }
- 方式二
<?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="aspectAroundParams" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.aopParams(String,int)) and args(name,times)"></aop:around> </aop:aspect> </aop:config> </beans>package com.jing.spring.aop;
/**
* 業務代碼
*/
public class IKAspectBiz {
public void aopParams(String name,int times){
System.out.println("IKAspectBiz.aopParams");
}
}package com.jing.spring.aop; import org.aspectj.lang.ProceedingJoinPoint; /** * 切面代碼 */ public class IKAspect { public void aspectAroundParams(ProceedingJoinPoint pj,String name,int times){ System.out.println("IKAspect.aspectAroud,its 環繞通知前:"); System.out.println("IKAspect.aspectAroud,"+name+"-----"+times); try { Object proceed = pj.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println("IKAspect.aspectAroud,its 環繞通知后"); System.out.println("IKAspect.aspectAroud,"+name+"-----"+times); } }
- Next
謹記
- 切面中的參數來源是從業務中取得的,也就是說,只有在切點中的參數,才會傳送到相對應的切面中,所以,切面中方法參數和切點時機的參數要保持對應。
<aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.aopParams(String,int)) and args(name,times)"></aop:pointcut>
<aop:around method="aspectAroundParams" pointcut-ref="ikPoint"></aop:around>
- (以環繞通知舉例)上述中的args(name,times)中的參數name對應切面IKAspect 類中的aspectAroundParams(ProceedingJoinPoint pj,String name,int times)方法中的參數,並且參數名稱和順序需要與切面中的通知方法參數名稱和順序一致。
- (以環繞通知舉例)上述中的aopParams(String,int)中的參數是對應業務IKAspectBiz 類中的aopParams(String name,int times)方法中的參數類型。