标签:except src 配置文件 instance 示例 signature ali term icc
示例:
applicationContext-xml.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> 7 8 <!-- 配置Bean --> 9 <bean id="arithmeticCalculator" 10 class="com.hk.spring.aop.xml.ArithmeticCalculatorImpl"> 11 </bean> 12 13 <!-- 配置切面的Bean --> 14 <bean id="loggingAspect" 15 class="com.hk.spring.aop.xml.LoggingAspect"> 16 </bean> 17 18 <bean id="vlidationAspect" 19 class="com.hk.spring.aop.xml.VlidationAspect"> 20 </bean> 21 22 <!-- 配置AOP --> 23 <aop:config> 24 <!-- 配置切点表达式 --> 25 <aop:pointcut expression="execution(* com.hk.spring.aop.xml.ArithmeticCalculator.*(int,int))" 26 id="pointcut"/> 27 <!-- 配置切面和通知 --> 28 <aop:aspect ref="loggingAspect" order="2" > 29 <aop:before method="beforeMethod" pointcut-ref="pointcut"/> 30 <aop:after method="afterMethod" pointcut-ref="pointcut"/> 31 <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/> 32 <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result" /> 33 </aop:aspect> 34 <aop:aspect ref="vlidationAspect" order="1"> 35 <aop:before method="validateArgs" pointcut-ref="pointcut"/> 36 </aop:aspect> 37 </aop:config> 38 </beans>
注:重点在代码里。
VlidationAspect.java:
1 package com.hk.spring.aop.xml; 2 3 import java.util.Arrays; 4 5 import org.aopalliance.intercept.Joinpoint; 6 import org.aspectj.lang.JoinPoint; 7 import org.aspectj.lang.annotation.Aspect; 8 import org.aspectj.lang.annotation.Before; 9 import org.springframework.core.annotation.Order; 10 import org.springframework.stereotype.Component; 11 12 13 public class VlidationAspect { 14 15 public void validateArgs(JoinPoint joinPoint){ 16 System.out.println("-->validate:" + Arrays.asList(joinPoint.getArgs())); 17 } 18 19 }
LoggingAspect.java:
1 package com.hk.spring.aop.xml; 2 3 import java.util.Arrays; 4 import java.util.List; 5 6 import org.aopalliance.intercept.Joinpoint; 7 import org.aspectj.lang.JoinPoint; 8 import org.aspectj.lang.ProceedingJoinPoint; 9 import org.aspectj.lang.annotation.After; 10 import org.aspectj.lang.annotation.AfterReturning; 11 import org.aspectj.lang.annotation.AfterThrowing; 12 import org.aspectj.lang.annotation.Around; 13 import org.aspectj.lang.annotation.Aspect; 14 import org.aspectj.lang.annotation.Before; 15 import org.aspectj.lang.annotation.Pointcut; 16 import org.springframework.core.annotation.Order; 17 import org.springframework.stereotype.Component; 18 19 20 public class LoggingAspect { 21 22 /* 23 * 定义一个方法,用于声明切入点表达式,一般该方法中再不需要加入其它代码 24 */ 25 public void declareJointPointExpression(){ 26 27 } 28 29 30 //声明该方法是一个前置通知:在目标方法之前执行 31 public void beforeMethod(JoinPoint joinPoint){ 32 String methodName = joinPoint.getSignature().getName(); 33 List<Object> args = Arrays.asList(joinPoint.getArgs()); 34 System.out.println("The method " + methodName + " begins with " + args); 35 } 36 37 //后置通知:在目标方法执行后(无论是否发生异常),执行的通知。 38 public void afterMethod(JoinPoint joinPoint){ 39 String methodName = joinPoint.getSignature().getName(); 40 System.out.println("The method " + methodName + " ends"); 41 } 42 43 44 /* 45 * 在方法正常执行后执行的通知叫返回通知 46 * 返回通知是可以访问到方法的返回值的 47 */ 48 public void afterReturning(JoinPoint joinPoint,Object result){ 49 String methodName = joinPoint.getSignature().getName(); 50 System.out.println("The method " + methodName + " ends with " + result); 51 } 52 53 /* 54 * 在目标方法出现异常时,会执行代码。 55 * 可以访问到异常对象;且可以指定在出现特定异常时在执行通知 56 */ 57 public void afterThrowing(JoinPoint joinPoint,Exception ex){ 58 String methodName = joinPoint.getSignature().getName(); 59 System.out.println("The method " + methodName + " coours exception : " + ex); 60 } 61 62 /* 63 * 环绕通知需要携带ProceedingJoinPoint 类型的参数 64 * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint这个类型的参数可以决定是否执行目标方法 65 * 且环绕通知必须有返回值,返回值即为目标方法的返回值 66 */ 67 public Object aroundMethod(ProceedingJoinPoint pjd){ 68 69 Object result = null; 70 String methodName = pjd.getSignature().getName(); 71 72 //执行目标方法 73 try { 74 //前置通知 75 System.out.println("The method " + methodName + "begins with " + Arrays.asList(pjd.getArgs())); 76 result = pjd.proceed(); 77 //后置通知 78 System.out.println("The method " + methodName + "ends with " + result); 79 } catch (Throwable e) { 80 //异常通知 81 System.out.println("The method occurs exception:" + e); 82 } 83 //后置通知 84 System.out.println("The method " + methodName + " ends"); 85 return result; 86 } 87 }
ArithmeticCalculator.java:
1 package com.hk.spring.aop.xml; 2 3 public interface ArithmeticCalculator { 4 int add(int i,int j); 5 int sub(int i,int j); 6 int mul(int i,int j); 7 int div(int i,int j); 8 }
ArithmeticCalculatorImpl.java:
1 package com.hk.spring.aop.xml; 2 3 import org.springframework.stereotype.Component; 4 5 6 public class ArithmeticCalculatorImpl implements ArithmeticCalculator { 7 8 @Override 9 public int add(int i, int j) { 10 int result = i + j; 11 return result; 12 } 13 14 @Override 15 public int sub(int i, int j) { 16 int result = i - j; 17 return result; 18 } 19 20 @Override 21 public int mul(int i, int j) { 22 int result = i * j; 23 return result; 24 } 25 26 @Override 27 public int div(int i, int j) { 28 int result = i / j; 29 return result; 30 } 31 32 }
运行结果:
标签:except src 配置文件 instance 示例 signature ali term icc
原文地址:https://www.cnblogs.com/zhzcode/p/9690971.html