spring AOP定義AfterThrowing增加處理例項分析
本文例項講述了spring AOP定義AfterThrowing增加處理。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?> <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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 指定自動搜尋Bean元件、自動搜尋切面類 --> <context:component-scan base-package="org.crazyit.app.service,org.crazyit.app.aspect"> <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect" /> </context:component-scan> <!-- 啟動@AspectJ支援 --> <aop:aspectj-autoproxy /> </beans>
二 切面類
package org.crazyit.app.aspect; import org.aspectj.lang.annotation.*; import org.aspectj.lang.*; // 定義一個切面 @Aspect public class RepairAspect { // 匹配org.crazyit.app.service.impl包下所有類的、 // 所有方法的執行作為切入點 @AfterThrowing(throwing="ex",pointcut="execution(* org.crazyit.app.service.impl.*.*(..))") // 宣告ex時指定的型別會限制目標方法必須丟擲指定型別的異常 // 此處將ex的型別宣告為Throwable,意味著對目標方法丟擲的異常不加限制 public void doRecoveryActions(Throwable ex) { System.out.println("目標方法中丟擲的異常:" + ex); System.out.println("模擬Advice對異常的修復..."); } }
三 介面
Hello
package org.crazyit.app.service; public interface Hello { // 定義一個簡單方法,模擬應用中的業務邏輯方法 void foo(); // 定義一個addUser()方法,模擬應用中的新增使用者的方法 int addUser(String name,String pass); }
World
package org.crazyit.app.service; public interface World { // 定義一個簡單方法,模擬應用中的業務邏輯方法 public void bar(); }
四 實現類
HelloImpl
package org.crazyit.app.service.impl; import org.springframework.stereotype.Component; import org.crazyit.app.service.*; @Component("hello") public class HelloImpl implements Hello { // 定義一個簡單方法,模擬應用中的業務邏輯方法 public void foo() { System.out.println("執行Hello元件的foo()方法"); } // 定義一個addUser()方法,模擬應用中的新增使用者的方法 public int addUser(String name,String pass) { System.out.println("執行Hello元件的addUser新增使用者:" + name); if(name.length() < 3 || name.length() > 10) { throw new IllegalArgumentException("name引數的長度必須大於3,小於10!"); } return 20; } }
WorldImpl
package org.crazyit.app.service.impl; import org.springframework.stereotype.Component; import org.crazyit.app.service.*; @Component("world") public class WorldImpl implements World { // 定義一個簡單方法,模擬應用中的業務邏輯方法 public void bar() { System.out.println("執行World元件的bar()方法"); } }
五 測試類
package lee; import org.springframework.context.*; import org.springframework.context.support.*; import org.crazyit.app.service.*; public class BeanTest { public static void main(String[] args) { // 建立Spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Hello hello = ctx.getBean("hello",Hello.class); hello.foo(); hello.addUser("悟空","7788"); World world = ctx.getBean("world",World.class); world.bar(); } }
六 測試結果
執行Hello元件的foo()方法
執行Hello元件的addUser新增使用者:悟空
目標方法中丟擲的異常:java.lang.IllegalArgumentException: name引數的長度必須大於3,小於10!
模擬Advice對異常的修復...
Exception in thread "main" java.lang.IllegalArgumentException: name引數的長度必須大於3,小於10!
at org.crazyit.app.service.impl.HelloImpl.addUser(HelloImpl.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:58)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy7.addUser(Unknown Source)
at lee.BeanTest.main(BeanTest.java:26)
更多關於java相關內容感興趣的讀者可檢視本站專題:《Spring框架入門與進階教程》、《Java資料結構與演算法教程》、《Java操作DOM節點技巧總結》、《Java檔案與目錄操作技巧彙總》和《Java快取操作技巧彙總》
希望本文所述對大家java程式設計有所幫助。