正常情况下 , 基于注解的生命要优先于基于 XML 的声明。
通过 AspectJ 注解 , 切面可以与 AspectJ 兼容 , 而基于 XML 的配置则是 Spring 专有的。由于 AspectJ 得到越来越多的 AOP 框架支持 , 所以以注解风格编写的切面将会有更多的重用机会。
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.xsd"> 7 8 9 <!-- 配置 ArithmeticImpl 的 bean --> 10 <bean id="arithmetic" class="com.itdoc.spring.aop.cutface.ArithmeticImpl"/> 11 12 <!-- 配置切面的 bean --> 13 <bean id="asjectLogging" class="com.itdoc.spring.aop.cutface.AsjectLogging"/> 14 <bean id="validate" class="com.itdoc.spring.aop.cutface.Validate"/> 15 16 <!-- 配置 AOP --> 17 <aop:config> 18 <!-- 配置切点表达式 --> 19 <aop:pointcut id="pointcut" expression="execution(* com.itdoc.spring.aop.cutface.*.*(..))"/> 20 <!-- 配置切面通知 --> 21 <aop:aspect ref="asjectLogging" order="2"> 22 <aop:around method="around" pointcut-ref="pointcut"/> 23 <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/> 24 <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/> 25 </aop:aspect> 26 <aop:aspect ref="validate" order="1"> 27 <aop:before method="beforeValidate" pointcut-ref="pointcut"/> 28 </aop:aspect> 29 30 </aop:config> 31 </beans>
1 package com.itdoc.spring.aop.cutface; 2 3 /** 4 * http://www.cnblogs.com/goodcheap 5 * 6 * @author: Wáng Chéng Dá 7 * @create: 2017-03-03 19:34 8 */ 9 public interface Arithmetic { 10 11 int add(int i, int j); 12 13 int sub(int i, int j); 14 15 int mul(int i, int j); 16 17 int div(int i, int j); 18 19 }
1 package com.itdoc.spring.aop.cutface; 2 3 4 /** 5 * http://www.cnblogs.com/goodcheap 6 * 7 * @author: Wáng Chéng Dá 8 * @create: 2017-03-03 19:35 9 */ 10 public class ArithmeticImpl implements Arithmetic { 11 @Override 12 public int add(int i, int j) { 13 int result = i + j; 14 return result; 15 } 16 17 @Override 18 public int sub(int i, int j) { 19 int result = i - j; 20 return result; 21 } 22 23 @Override 24 public int mul(int i, int j) { 25 int result = i * j; 26 return result; 27 } 28 29 @Override 30 public int div(int i, int j) { 31 int result = i / j; 32 return result; 33 } 34 }
1 package com.itdoc.spring.aop.cutface; 2 3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.ProceedingJoinPoint; 5 import java.util.Arrays; 6 7 /** 8 * 通知 9 * http://www.cnblogs.com/goodcheap 10 * 11 * @author: Wáng Chéng Dá 12 * @create: 2017-03-04 9:50 13 */ 14 15 public class AsjectLogging { 16 17 public void afterReturning(JoinPoint joinPoint, Object result) { 18 Object methodName = joinPoint.getSignature().getName(); 19 System.out.println("The method " + methodName + " ends with " + result); 20 } 21 22 public void afterThrowing(JoinPoint joinPoint, Exception e) { 23 Object methodName = joinPoint.getSignature().getName(); 24 System.out.println("The method " + methodName + " exception with " + e); 25 } 26 27 public Object around(ProceedingJoinPoint point) { 28 Object methodName = point.getSignature().getName(); 29 Object[] args = point.getArgs(); 30 Object result = null; 31 try { 32 //前置通知 33 System.out.println("The method " + methodName + " begins with" + Arrays.asList(args)); 34 //执行方法 35 result = point.proceed(); 36 //返回通知 37 System.out.println("The method " + methodName + " ends with " + result); 38 } catch (Throwable e) { 39 e.printStackTrace(); 40 //异常通知 41 System.out.println("The method " + methodName + " exception with " + e); 42 } finally { 43 //后置通知 44 System.out.println("The method " + methodName + " ends"); 45 } 46 return result; 47 } 48 }
1 package com.itdoc.spring.aop.cutface; 2 3 4 /** 5 * http://www.cnblogs.com/goodcheap 6 * 7 * @author: Wáng Chéng Dá 8 * @create: 2017-03-04 12:20 9 */ 10 public class Validate { 11 12 public void beforeValidate() { 13 System.out.println("I am Validate's beforeValidate method..."); 14 } 15 }
1 package com.itdoc.spring.aop.cutface; 2 3 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 /** 8 * http://www.cnblogs.com/goodcheap 9 * 10 * @author: Wáng Chéng Dá 11 * @create: 2017-03-04 9:54 12 */ 13 public class Main { 14 15 public static void main(String[] args) { 16 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationConfig.xml"); 17 Arithmetic arithmetic = (Arithmetic) ctx.getBean("arithmetic"); 18 System.out.println("result = " + arithmetic.div(1, 1)); 19 } 20 }
控制台输出:
I am Validate's beforeValidate method... |