您的位置:首页 > 编程语言 > Java开发

20Spring切面的优先级

2015-06-23 15:15 525 查看
通过使用@order注解指定切面的优先级,值越小,优先级越高
代码:


packagecom.cn.spring.aop.impl;
//加减乘除的接口类
publicinterfaceArithmeticCalculator{
intadd(inti,intj);
intsub(inti,intj);
intmul(inti,intj);
intdiv(inti,intj);
}




packagecom.cn.spring.aop.impl;

importorg.springframework.stereotype.Component;

//实现类
@Component
publicclassArithmeticCalculatorImplimplementsArithmeticCalculator{
@Override
publicintadd(inti,intj){
intresult=i+j;
returnresult;
}

@Override
publicintsub(inti,intj){
intresult=i-j;
returnresult;
}

@Override
publicintmul(inti,intj){
intresult=i*j;
returnresult;
}

@Override
publicintdiv(inti,intj){
intresult=i/j;
returnresult;
}
}


packagecom.cn.spring.aop.impl;

importorg.aspectj.lang.JoinPoint;
importorg.aspectj.lang.ProceedingJoinPoint;
importorg.aspectj.lang.annotation.*;
importorg.springframework.core.annotation.Order;
importorg.springframework.stereotype.Component;

importjava.util.Arrays;
importjava.util.List;

//把这个类声明为一个切面:首先需要把该类放入到IOC容器中,在声明为一个切面
//可以使用@order注解指定切面的优先级,值越小,优先级越高
@Order(2)
@Aspect
@Component
publicclassLoggingAspect{

//声明该方法是一个前置通知:在目标方法开始之前执行
@Before("execution(publicintArithmeticCalculator.*(int,int))")
publicvoidbeforeMethod(JoinPointjoinPoint){
StringmethodName=joinPoint.getSignature().getName();
List<Object>args=Arrays.asList(joinPoint.getArgs());

System.out.println("Themethod"+methodName+"beginswith"+args);
}

//后置通知:在目标方法执行后(无论是否发生异常),执行的通知
//在后置通知中还不能访问目标方法执行的结果
@After("execution(publicintArithmeticCalculator.*(int,int))")
publicvoidafterMethod(JoinPointjoinPoint){
StringmethodName=joinPoint.getSignature().getName();
List<Object>args=Arrays.asList(joinPoint.getArgs());

System.out.println("Themethod"+methodName+"endswith"+args);
}

/**
*在方法正常结束后执行的代码
*返回通知是可以访问到方法的返回值
*@paramjoinPoint
*/
@AfterReturning(value="execution(publicintArithmeticCalculator.*(int,int))",
returning="result")
publicvoidafterReturning(JoinPointjoinPoint,Objectresult){
StringmethodName=joinPoint.getSignature().getName();
List<Object>args=Arrays.asList(joinPoint.getArgs());

System.out.println("Themethodendswitd"+result);
}

//在目标方法出现异常时会执行的代码
//可以访问到异常对象;且可以指定在出现特定异常时再执行通知代码
@AfterThrowing(value="execution(publicintArithmeticCalculator.*(int,int))",
throwing="ex")
publicvoidafterReturning(JoinPointjoinPoint,Exceptionex){
StringmethodName=joinPoint.getSignature().getName();

System.out.println("Themethod"+methodName+"occuresexceptionwith:"+ex);
}

/**
*环绕通知需要携带ProceedingJoinPoint类型的参数
*环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法
*且环绕通知必须有返回值,返回值为目标方法的返回值
*@paramproceedingJoinPoint
*/
@Around("execution(publicintArithmeticCalculator.*(int,int))")
publicObjectaroundMethod(ProceedingJoinPointproceedingJoinPoint){
Objectresult=null;
StringmethodName=proceedingJoinPoint.getSignature().getName();
//执行目标方法
try{
//前置通知
System.out.println("Themethod"+methodName+"beginswith"+Arrays.asList(proceedingJoinPoint.getArgs()));
result=proceedingJoinPoint.proceed();
//返回通知
System.out.println("Themethodendswith"+result);
}catch(Throwablethrowable){
//异常通知
System.out.println("Themethod"+methodName+"occuresexceptionwith:"+throwable);
thrownewRuntimeException(throwable);
}
//后置通知
System.out.println("Themethod"+methodName+"ends");
returnresult;
}
}


packagecom.cn.spring.aop.impl;

importorg.aspectj.lang.JoinPoint;
importorg.aspectj.lang.annotation.Aspect;
importorg.aspectj.lang.annotation.Before;
importorg.springframework.core.annotation.Order;
importorg.springframework.stereotype.Component;

importjava.util.Arrays;

/**
*Createdbyjecyhwon2015/6/21.
*/
@Order(1)
@Aspect
@Component
publicclassValidationAspect{

@Before("execution(publicintArithmeticCalculator.*(..))")
publicvoidvalidateArgs(JoinPointjoinPoint){
System.out.println("validate:"+Arrays.asList(joinPoint.getArgs()));
}
}


packagecom.cn.spring.aop.impl;

importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;

publicclassMain{
publicstaticvoidmain(String[]args){
//1.创建Spring的IOC容器
ApplicationContextctx=newClassPathXmlApplicationContext("17-1.xml");

//2.从IOC容器中huo获取bean的实例
ArithmeticCalculatorarithmeticCalculator=ctx.getBean(ArithmeticCalculator.class);

//3.使用bean
intresult=arithmeticCalculator.add(3,6);

System.out.println("result:"+result);
//result=arithmeticCalculator.div(3,0);
//System.out.println("result:"+result);
}
}


<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><context:component-scanbase-package="com.cn.spring.aop.impl">
</context:component-scan>

<!--使AspjectJ注解起作用:自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: