Spring AOP筆記

最近研究了下spring的aop,從spring2開始,spring支持基於XML和annotation的兩種配置,先來看看基於XML配置的實現。

基於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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<bean id="log" class="com.inter.GetLog"></bean>

<!--
注意這裏:expression="execution(* com.inter.*.print*(..))"
括號裏面第一個*號代表返回值 接下來 com.inter.*. 是你要切入的代碼的大概路徑,這裏爲什麼用大概路徑來形容呢
因爲這裏的意思是符合以com.inter的路徑都會作爲選擇的對象,也不詳細介紹,查下就明白了, print*(..)是指
方法名以print開頭的都符合,括號裏面的 .. 表示參數是隨意的都可以。
-->
<aop:config>
<aop:aspect ref="log">
<aop:pointcut id="printMethods" expression="execution(* com.inter.*.print*(..))"/>
<aop:around method="getLog" pointcut-ref="printMethods"/>
</aop:aspect>
</aop:config>

<aop:config>
<aop:aspect ref="log">
<aop:pointcut id="dealException" expression="execution(* com.inter.*.excep*(..))"/>
<aop:around method="getLog" pointcut-ref="dealException"/>
</aop:aspect>
</aop:config>

<aop:config>
<aop:aspect ref="log">
<aop:pointcut id="before" expression="execution(* com.inter.*.bef*(..))"/>
<aop:before method="before" pointcut-ref="before"/>
</aop:aspect>
</aop:config>


<!--要織入代碼的bean-->
<bean id="print" class="com.inter.SystemPrint"></bean>

</beans>

SystemPrin的實現類:

public class SystemPrint implements Print{

public String print(String name){
String result="hello " + name;
System.out.println(result);
return result;
}

public String before(String name){
String result="before " + name;
System.out.println(result);
return result;
}

public void exception(String name) throws Exception{
System.out.println(name);
throw new Exception();
}
}

下面是所要織入的代碼,也就是我們要用來打印和處理異常:

import org.aspectj.lang.ProceedingJoinPoint;
public class GetLog {
public void getLog(ProceedingJoinPoint joinpoint) throws Throwable {
System.out.println("===begin===");
try{
joinpoint.proceed();
}
catch(Exception e)
{
System.out.println("log exception!");
}
System.out.println("===end=====");
}

public void before(JoinPoint joint) throws Throwable
{
System.out.println("===begin===");
System.out.println("before!");
}

}

編寫一個測試類,分別測試上面兩個方法,可以得到如下的結果:

===begin===
hello ding
===end=====
-----------------
===begin===
laoding
log exception!
===end=====
-----------------
===begin===
before!
before beforeing

下面來看看基於Annotation的實現:

<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<!--基於@AspectJ切面的驅動器,如果沒有這句話 切面的代碼是不會執行的,可以試下-->
<aop:aspectj-autoproxy/>


<bean id="myAspect" class="com.inter.MyAspect"></bean>
<bean id="hello" class="com.inter.HelloImpl"></bean>

</beans>

HelloImpl類的實現:

public class HelloImpl implements Hello {
public String hello(String name)
{
String say = "Hello," + name;

System.out.println(say);
return say;
}
}

切面的實現:

//首先這是註釋這個類就是切面
@Aspect
public class MyAspect {
/*這裏是註釋要切入的方法,AfterReturning是表示方法返回以後進行切入,我這裏
選這個的話是因爲日誌一般都是在方法執行完成後記錄,當然你可以拿Before來試*/
@Around("execution(* com.inter.*.hello(..))")
public void doLog(ProceedingJoinPoint joinpoint) throws Throwable{
String result = (String)joinpoint.proceed();
System.out.println("---doLog"+result);
}
}

然後編寫一個測試類,得到如下測試結果:

-----------------
Hello,xiao
---doLogHello,xiao

發佈了96 篇原創文章 · 獲贊 1 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章