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/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://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>
相关文章推荐
- 安装 JDK 1.6 在 RHEL x64
- 《Java并发编程实践》笔记1——并发编程基础
- Java编程中“为了性能”需做的26件事
- JAVA菜鸟入门(18) Set的Iterator
- MyEclipse卡死解决
- 基于Spring框架的Shiro配置
- java生成二维码
- Java笔记:for循环
- eclipse-debug时直接进入/不进入/提示进入调试页面修改
- 在java中实现日期类型和字符串类型的转换大全(Date String Timestamp Datetime)
- Lombok — Java代码自动生成 开发利器 Eclipse for Mac
- Java笔记--1
- Java输入输出流和文件操作
- JAVA_HOME从jdk8变到jdk7错误:Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersi
- SSM(Spring+SpringMVC+MyBatis)整合
- spring-data-jpa
- 深入分析Java Web技术内幕_第二章 学习笔记
- java.lang.OutOfMemoryError: Java heap space错误和方法(集、转)
- jdk:HashSet基于HashMap实现
- Eclipse自动生成作者、日期注释等功能