1. 程式人生 > >採用xml配置方式進行配置AOP程式設計

採用xml配置方式進行配置AOP程式設計

1.//驗證切面優先順序,數越小優先順序越高
@Order(2)
2.
/*定義一個方法, 宣告切入點表示式,一般地。該方法中再不需要新增其他程式碼,這就
* 相當於一個常量其他切點需要的時候。我們只需要在切點生命出引用該方法即可.
* 在其他切點類中我們也可以應用這個方法。引用格式就是在需要的切入點加上:類名+此方法名
* 如果兩個類不在同一個包下。我們還需要加入此類的全類名
* eg:@Before("LoggingAspect.declareJointPointExpression()")
*/
@Pointcut("execution(public int com.test.spring.aop.impl.ArithmeticCalaculator.*(..))")
public void declareJointPointExpression(){}
可以引用該類中的宣告的切入點表示式
@Before("declareJointPointExpression()")
public void beforemethod(JoinPoint joint) {
String methodname = joint.getSignature().getName();
Object[] args = joint.getArgs();
System.out.println("the mehtod" + methodname + " begins with:"
+ Arrays.asList(args));
}

//如果在xml檔案中配置AOP。
首先建立一個application-xml.xmlspring配置檔案。
此時我們已經建立好一個Arithmetic介面和實現該介面的ArithmeticImpl的類。
public  void  add()
public  void  min()
public  void  mul()
public  void  div()
上邊實現其中的方法
我們也建立了一個Aspect切面類。
package com.test.spring.aop.impl.xml;

import java.util.Arrays;

import javax.management.RuntimeErrorException;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

//此類是切面所以要加一個特殊註解
//驗證切面優先順序,數越小優先順序越高
public class LoggingAspect {
/*定義一個方法, 宣告切入點表示式,一般地。該方法中再不需要新增其他程式碼,這就
* 相當於一個常量其他切點需要的時候。我們只需要在切點生命出引用該方法即可.
* 在其他切點類中我們也可以應用這個方法。引用格式就是在需要的切入點加上:類名+此方法名
* 如果兩個類不在同一個包下。我們還需要加入此類的全類名
* eg:@Before("LoggingAspect.declareJointPointExpression()")
*/
public void declareJointPointExpression(){}
/*
* 在com.test.spring.aop.ArithmeticCalactulato介面每個實現類的每一個方法 使之前執行一段程式碼
* ArithmeticCalaculator 前置通知
*/
public void beforeMthod(JoinPoint joint) {
String methodname = joint.getSignature().getName();
Object[] args = joint.getArgs();
System.out.println("繼續xml檔案配置的aop");
System.out.println("the mehtod" + methodname + " begins with:"
+ Arrays.asList(args));

}

/*
* 後置通知 在目標方法執行之後進行執行
*/
public void afterMethod(JoinPoint joint) {
// 獲取目標方法名稱
String methodname = joint.getSignature().getName();
Object[] obj = joint.getArgs();
System.out.println("the aftermethod method 基於xml配置方式:" + methodname + "  end with args"
+ Arrays.asList(obj));
}

/*
* 有些方法是在目標方法正常執行之後執行。返回通知 返回通知是可以訪問到方法的返回值的。
*/
public void afterReturnMethod(JoinPoint point, Object result) {
String mehtodname = point.getSignature().getName();
System.out.println("the method--基於xml配置的執行後返回的方法----after---- bengin" + mehtodname
+ " end with:" + result);

}

/*
* 在目標方法出現異常時候執行的程式碼 可以訪問到異常;且可以指定異常的型別。 異常通知。如果目標方法中存在異常。這時候。會執行這個方法
*/
public void afterThrowing(JoinPoint point, Exception ex) {
String mehtodname = point.getSignature().getName();
System.out.println("the 丟擲異常   method------ bengin" + mehtodname
+ " occur with:" + ex);
}

/*
* Around環繞通知 環繞通知執行的方法需要在引數裡使用ProcedingJoinPoin型別的引數
* 其中環繞通知類似於動態代理的全過程。就是ProcedingJoinPoint型別的引數決定是否執行目標通知
* 且環繞通知必須有返回值。返回值即為目標方法的返回值。
*/
/*
* 只做瞭解即可
*/
public Object aroundMethod(ProceedingJoinPoint pro) {
// 執行目標方法
Object result = null;
String methodname = pro.getSignature().getName();
try {
// 前置通知
System.out.println("Th==-=-=-start-=-=-= method" + methodname
+ "begins with" + Arrays.asList(pro.getArgs()));
result = pro.proceed();
// 後置通知
System.out.println("Th==-=-=-end=-=-=-= method" + methodname
+ "end with" + result);
} catch (Throwable e) {
// 異常通知
System.out.println("The method occurs exception" + e);
throw new RuntimeErrorException((Error) e);
}
System.out.println(" the aroundMethod is" + methodname + "ends");
return result;

}
}
下邊我們就開始配置xml檔案進行配置AOP切面程式設計。
1.首先建立一個bean  該bean是我們要操作目標方法的類
<!-- 配置bean -->
<bean id="arithmeticCalaculator" class="com.test.spring.aop.impl.xml.ArithmeticCalculatorImpl"></bean>
2.建立一個切面bean.這個bean是我們的Aspect類
<!-- 配置切面的bean -->
<bean id="loggingAspect" class="com.test.spring.aop.impl.xml.LoggingAspect"></bean>
3.下邊開始配置AOP
1)首先配置切點表示式<aop:pointcut>。這裡的expresss屬性值就是執行目標方法包含引數型別,返回值。我們也可以用(..)代替引數及其型別
其中*代表所有。及所有的返回型別。該類下的所有方法。其中id我們可以隨意寫。
2)其次就是配置切面<aop:aspect>這裡的ref屬性值就是對應的切面類裡對應切面類bean配置的id屬性值
2-1)配置前置/後置/異常/返回值方法<aop:before><aop:after><aop:after-returning>這些配置都需要有point-ref屬性
其屬性值就是pointcut-ref屬性值就是我們配置切點的id值。其他對應的returning屬性之就是方法的返回值。丟擲異常的值throwing為方法中的
異常型別引數的值。
<aop:config>
<!--配置切點表示式, -->
<aop:pointcut
expression="execution(* com.test.spring.aop.impl.xml.ArithmeticCalaculator.*(int,int))"
id="pointcut" />
<!-- 配置切面(ref的值就是bean的id值)及其通知 -->
<aop:aspect ref="loggingAspect" order="1"><!-- 
<aop:before method="beforeMthod" pointcut-ref="pointcut" />
<aop:after method="afterMethod" pointcut-ref="pointcut" />
<aop:after-returning method="afterReturnMethod" pointcut-ref="pointcut"  returning="result"/>
<aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="pointcut"/> -->
<aop:around method="aroundMethod" pointcut-ref="pointcut" />
</aop:aspect>
</aop:config>