spring筆記--使用springAPI實現AOP的一個簡單例子,springapiaop
Spring的另一個重要思想是AOP,面向切面的編程,它提供了一種機制,可以在執行業務前後執行另外的代碼,Servlet中的Filter就是一種AOP思想的體現,下面通過一個例子來感受一下.
假設我們現在需要在針對資料庫進行CRUD操作時添加一組日誌,即在執行CRUD方法前後分別加上一句話,實現簡單的面向切面編程的功能.我用到的是spring4,在設定檔上較之之前的版本可能有些不同.
使用springAPI來實現AOP,除了spring必不可少的核心jar包,還需要兩個jar包需要匯入:
並且設定檔的標頭檔也需要略作修改,詳見下面執行個體中的beans.xml.
UserService類(省略資料庫作業碼,只做簡單的列印來類比):
public class UserService { public void add(){ System.out.println("添加使用者"); } public void delete(){ System.out.println("刪除使用者"); } public void update(){ System.out.println("修改使用者"); } public void search(){ System.out.println("查詢使用者"); }}
Log類(在執行增刪改查方法前執行):
public class Log implements MethodBeforeAdvice{ /*** * method:被調用的方法對象 * arg1:被調用的方法的參數 * target:被呼叫者法的目標對象 */ @Override public void before(Method method, Object[] arg1, Object target) throws Throwable { System.out.println(target.getClass().getName()+"中的"+method.getName()+"方法被執行"); }}
AfterLog類(在執行增刪改查方法後執行):
public class AfterLog implements AfterReturningAdvice{ /** * returnValue:傳回值類型 * method:被調用的方法對象 * arg1:被調用的方法的參數 * target:被呼叫者法的目標對象 */ @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName()+"中的"+method.getName()+"方法被執行成功,傳回值是"+returnValue); }
spring設定檔(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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="com.wang.service.UserService"></bean> <bean id="log" class="com.wang.log.Log"></bean> <bean id="afterlog" class="com.wang.log.AfterLog"></bean> <aop:config> <!--"*"為萬用字元表示所有方法 ".."表示任意個數的參數 --> <aop:pointcut expression="execution(* com.wang.service.UserService.*())" id="pointcut"/> <aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/> </aop:config></beans>
測試代碼testDemo:
@Test public void test1(){ ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml"); UserService userService=(UserService)context.getBean("userService"); userService.add(); userService.search(); }
運行測試可以看到控制台的列印結果:
com.wang.service.UserService中的add方法被執行
添加使用者
com.wang.service.UserService中的add方法被執行成功,傳回值是null
com.wang.service.UserService中的search方法被執行
查詢使用者
com.wang.service.UserService中的search方法被執行成功,傳回值是null
在設定檔中,我們看到了一些這樣的代碼:
<aop:config> <!--"*"為萬用字元表示所有方法 ".."表示任意個數的參數 --> <aop:pointcut expression="execution(* com.wang.service.UserService.*())" id="pointcut"/> <aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/> </aop:config>
我們來介紹幾個AOP的相關概念,這個就比較好理解了:
- 切面(Aspect):在本例中.add(),delete(),等方法中都有一些代碼,在真實的程式中這裡不會只是簡單的列印語句而是一些有意義的代碼,這些代碼可以看做是AOP的切面.
- 通知(Advisor):本例中的兩個日誌類,這裡可以成為攔截器,都是實現了某個*Advisor介面,這兩個類就是指AOP中的通知,一旦Spring符合了條件,就會發出通知,與生活中我們所說的通知不同的是,Spring中的通知是帶有執行代碼的,能實現某種功能.
- 切入點(pointcut):在配置攔截器(<aop-config>)的時候,xml中配置了UserService中所有的方法都是用攔截器,這個配置是通過spring中的一個已經寫好的類完成的,這個類能配置對哪些方法使用攔截器,從那個地方"切入"進去.配置是可以使用萬用字元.
簡而言之:"切入點"負責往"什麼地方"插入代碼,"通知"負責插入"什麼代碼".