Pointcut与Advisor实现AOP
在研究事务的时候发现事务的AOP类TransactionInterceptor是实现了接口MethodInterceptor,于是想着研究下这种AOP的实现方式。
1. 实现方式1如下
1.实现MethodInterceptor接口。(扮演Advice通知的角色)
简单的实现方法执行前、执行后打印一下日志。
package cn.qlq.aop; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class MyInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println(invocation.getMethod() + "==方法执行前=="); Object proceed = invocation.proceed(); System.out.println(invocation.getArguments() + "--方法执行后--"); return proceed; } }
其实MethodInterceptor接口继承Interceptor、Interceptor接口继承Advice接口。这些接口是org.aopalliance包下的(spring-aop包)
2.实现接口Pointcut(扮演切入点的角色)
根据类名称、方法名称判断是否满足指定的过滤条件。
package cn.qlq.aop; import java.lang.reflect.Method; import org.apache.commons.lang3.StringUtils; import org.springframework.aop.ClassFilter; import org.springframework.aop.MethodMatcher; import org.springframework.aop.Pointcut; public class MyPoint implements Pointcut { @Override public ClassFilter getClassFilter() { return new ClassFilter() { @Override public boolean matches(Class<?> clazz) { // 如果ServiceImpl就返回true if (clazz != null && StringUtils.contains(clazz.getName(), "cn.qlq.service")) { System.out.println("======MyPoint======getClassFilter "); System.out.println(clazz); return true; } return false; } }; } @Override public MethodMatcher getMethodMatcher() { return new MethodMatcher() { /** * 判断方法是否匹配 */ @Override public boolean matches(Method method, Class<?> targetClass, Object... args) { System.out.println("matches=========1"); System.out.println(method); System.out.println(targetClass); System.out.println(args); return true; } /** * 判断方法是否匹配 */ @Override public boolean matches(Method method, Class<?> targetClass) { System.out.println("matches=========2"); System.out.println(method); System.out.println(targetClass); return true; } @Override public boolean isRuntime() { return false; } }; } }
3.增加配置类实现PointcutAdvisor。注意要注入到spring容器中。(扮演切面Aspect的角色)
package cn.qlq.aop; import org.aopalliance.aop.Advice; import org.springframework.aop.Pointcut; import org.springframework.aop.PointcutAdvisor; import org.springframework.stereotype.Component; @Component public class MyAdvicer implements PointcutAdvisor { @Override public Advice getAdvice() { return new MyInterceptor(); } @Override public boolean isPerInstance() { return true; } @Override public Pointcut getPointcut() { return new MyPoint(); } }
2. 实现方式2如下:
增强类:
package cn.qlq.aop; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class MyInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println(invocation.getMethod() + "==方法执行前=="); Object proceed = invocation.proceed(); System.out.println(invocation.getArguments() + "--方法执行后--"); return proceed; } }
配置类:
package cn.qlq.aop; import org.springframework.aop.aspectj.AspectJExpressionPointcut; import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyInterceptorConfig { public static final String traceExecution = "execution(* cn.qlq.service..*.*(..))"; @Bean public DefaultPointcutAdvisor defaultPointcutAdvisor2() { AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(traceExecution); // 配置增强类advisor DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(); advisor.setPointcut(pointcut); MyInterceptor interceptor = new MyInterceptor(); advisor.setAdvice(interceptor); return advisor; } }
还有另一种方式就是自定义注解加@Aspect方式实现AOP。参考:https://www.cnblogs.com/qlqwjy/p/8729280.html
【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】