重用切面表达式
我们有的时候在切面里面有多个函数,大部分函数的切入点都是一样的,所以我们可以声明切入点表达式,来重用。
package logan.study.aop.impl; public interface ArithmeticCalculator { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); }
package logan.study.aop.impl; import org.springframework.stereotype.Component; @Component public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override public int add(int i, int j) { // TODO Auto-generated method stub int result = i + j; return result; } @Override public int sub(int i, int j) { // TODO Auto-generated method stub int result = i - j; return result; } @Override public int mul(int i, int j) { // TODO Auto-generated method stub int result = i * j; return result; } @Override public int div(int i, int j) { // TODO Auto-generated method stub int result = i / j; return result; } }
package logan.study.aop.impl; import java.util.Arrays; import java.util.List; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; 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; //把这个类声明为一个切面:需要把该类放入到IOC容器中,在声明为一个切面 @Order(2) @Aspect @Component public class LoggingAspect { /** * * 声明一个切入点表达式 * */ @Pointcut("execution(public int logan.study.aop.impl.ArithmeticCalculator.*(int, int))") public void declareJoinPointException(){} @Around("declareJoinPointException()") public Object aroundMethod(ProceedingJoinPoint pjd){ Object result = null; String methodName = pjd.getSignature().getName(); //执行目标方法 try{ //前置通知 System.out.println("The method" + methodName + " begins with " + Arrays.asList(pjd.getArgs())); result = pjd.proceed(); //返回通知 System.out.println("The method" + methodName + " ends with " + result); }catch(Throwable e){ //异常通知 System.out.println("The method occurs exception:" + e); } //后置通知 System.out.println("The method " + methodName + "ends"); return result; } }
package logan.study.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; //可以 @Order(1) @Aspect @Component public class VlidationAspect { @Before("LoggingAspect.declareJoinPointException()") public void validateArgs(JoinPoint joinPoint){ System.out.println("validate: " + Arrays.asList(joinPoint.getArgs())); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <context:component-scan base-package="logan.study.aop.impl"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
package logan.study.aop.impl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub //1.创建Spring的IOC容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //2.从IOC容器里面获取bean实例 ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class); //使用Bean int result = arithmeticCalculator.add(3, 6); System.out.println(result); result = arithmeticCalculator.div(3, 0); System.out.println(result); } }
返回结果:
五月 28, 2017 7:15:31 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@49c2faae: startup date [Sun May 28 07:15:31 CST 2017]; root of context hierarchy 五月 28, 2017 7:15:31 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] validate: [3, 6] The methodadd begins with [3, 6] The methodadd ends with 9 The method addends 9 validate: [3, 0] The methoddiv begins with [3, 0] The method occurs exception:java.lang.ArithmeticException: / by zero The method divends Exception in thread "main" org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int logan.study.aop.impl.ArithmeticCalculator.div(int,int) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:227) at com.sun.proxy.$Proxy10.div(Unknown Source) at logan.study.aop.impl.Main.main(Main.java:18)
可以看到我们在LoggingAspect里面声明的 declareJoinPointException可以起作用。一般情况下,这个方法不用写其他的代码。