Spring中的異常通知與後置通知怎麼回事啊?
1:介面
package com.aop.impl;
public interface AtithmeticCalculator {
int add(int i,int j);
int sub(int i,int j);
int mul(int i,int j);
int div(int i,int j);
}
2:實現類
package com.aop.impl;
import org.springframework.stereotype.Component;
@Component("atithmeticCalculator")
public class AtithmeticCalculatorImpl implements AtithmeticCalculator {
public int add(int i, int j) {
int result=i+j;
return result;
}
public int sub(int i, int j) {
int result=i-j;
return result;
}
public int mul(int i, int j) {
int result=i*j;
return result;
}
public int div(int i, int j) {
int result=i/j;
return result;
}
}
3:切面類
package com.aop.impl;
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
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;
/**
* 將功能相近的組成一個切面
* @author Administrator
* 把這個類宣告一個切面 :需要把這個類放到IOC容器中,再宣告為一個切面
*/
@Order(1)
@Aspect
@Component
public class LoggingAspect {
//重用切點表示式
//定義一個方法,用於宣告切點表示式,一般地,該方法中不需要新增其它程式碼
@Pointcut("execution(public int com.aop.impl.AtithmeticCalculatorImpl.*(int , int ))")
public void declareJointPointExpression()
{
}
//表示一個前置通知:在目標方法開始執行之前執行
@Before("declareJointPointExpression()")
public void beforeMethod(JoinPoint joinPoint)
{
//連線點:執行方法的引數名字
String methodName=joinPoint.getSignature().getName();
List<Object> args=Arrays.asList(joinPoint.getArgs());
System.out.println("The method "+methodName+" begins with"+args);
}
//表示一個後置通知:在目標方法執行之後執行(無論是否異常)都會執行
@After("declareJointPointExpression()")
public void afterMethod(JoinPoint joinPoint)
{
//連線點:執行方法的引數名字
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" ends");
}
//表示一個後置通知:在目標方法返回之前執行(發生異常不執行)
@AfterReturning(value="declareJointPointExpression()",returning="result")
public void afterReturningMethod(JoinPoint joinPoint,Object result)
{
//連線點:執行方法的引數名字
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" ends with "+result);
}
//表示一個異常通知:在發生一場之後執行(可以訪問到異常物件:可以指定在某個異常時才執行)
@AfterThrowing(value="declareJointPointExpression()",throwing="ex")
public void afterThrowingMethod(JoinPoint joinPoint,Exception ex)
{
//連線點:執行方法的引數名字
String methodName=joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" occurs exception: "+ex);
}
}
4:驗證類
package com.aop.impl;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
* 驗證切面
* @author Administrator
*
*/
@Order(2)
@Aspect
@Component
public class VLidationASpect {
//驗證通知(可以指定切面優先順序 Order(value)value越小,優先順序越高)
@Before("execution(public int com.aop.impl.AtithmeticCalculatorImpl.*(..))")
public void validateArgs(JoinPoint joinPoint)
{
System.out.println("-->validate:"+Arrays.asList(joinPoint.getArgs()));
}
}
5:測試類
package com.aop.impl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Spring的AOP(基於ASpectj的方式配置)
* 1).新增jar包
* 2). 在配置檔案中新增配置<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
* 3).把橫切關注點的程式碼放到切面類中(切面首先是IOC容器中的一個bean,即新增@Component
* 4).切面類還需要加入@Aspectj註解
* 5).在類裡宣告各種通知(Before,After,AfterReturning,AfterThrowing)
* execution(ASpectj的表示式:public int com.aop.impl.AtithmeticCalculatorImpl.*(int , int )))
* 6).joinPoint連線點:在連線方法中新增一個
* @author Administrator
*
*/
public class Main {
public static void main(String[] args) {
//1:建立Spring的IOC容器物件
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//2:從IOC容器中獲取Bean例項
AtithmeticCalculator atithmeticCalculator=(AtithmeticCalculator)ctx.getBean(AtithmeticCalculator.class);
//3:使用bean
int result=atithmeticCalculator.add(1,2);
System.out.println("result:"+result);
result=atithmeticCalculator.div(12,6);
System.out.println("result:"+result);
result=atithmeticCalculator.div(12,1);
System.out.println("result:"+result);
}
}