|
| 1 | +package org.springframework.wx.aop.intercept; |
| 2 | + |
| 3 | +import org.aopalliance.intercept.MethodInterceptor; |
| 4 | +import org.aopalliance.intercept.MethodInvocation; |
| 5 | +import org.jetbrains.annotations.NotNull; |
| 6 | +import org.jetbrains.annotations.Nullable; |
| 7 | +import org.springframework.aop.AfterReturningAdvice; |
| 8 | +import org.springframework.aop.aspectj.AspectJExpressionPointcut; |
| 9 | +import org.springframework.aop.support.DefaultPointcutAdvisor; |
| 10 | +import org.springframework.context.annotation.Bean; |
| 11 | +import org.springframework.context.annotation.Configuration; |
| 12 | +import org.springframework.context.annotation.EnableAspectJAutoProxy; |
| 13 | + |
| 14 | +import java.lang.reflect.Method; |
| 15 | + |
| 16 | +@Configuration |
| 17 | +@EnableAspectJAutoProxy(proxyTargetClass = true) |
| 18 | +public class AopConfig { |
| 19 | + |
| 20 | + @Bean |
| 21 | + public AspectJExpressionPointcut myAspectJExpressionPointcut() { |
| 22 | + AspectJExpressionPointcut aspectJExpressionPointcut = new AspectJExpressionPointcut(); |
| 23 | + aspectJExpressionPointcut.setExpression("execution(* org.springframework.wx.aop.RandomService.*(..))"); |
| 24 | + return aspectJExpressionPointcut; |
| 25 | + } |
| 26 | + |
| 27 | + @Bean |
| 28 | + public DefaultPointcutAdvisor defaultPointcutAdvisor(AspectJExpressionPointcut myAspectJExpressionPointcut) { |
| 29 | + TracingInterceptor tracingInterceptor = new TracingInterceptor(); |
| 30 | + DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor(); |
| 31 | + defaultPointcutAdvisor.setPointcut(myAspectJExpressionPointcut); |
| 32 | + defaultPointcutAdvisor.setAdvice(tracingInterceptor); |
| 33 | + return defaultPointcutAdvisor; |
| 34 | + } |
| 35 | + |
| 36 | + @Bean |
| 37 | + public DefaultPointcutAdvisor defaultAfterPointcutAdvisor(AspectJExpressionPointcut myAspectJExpressionPointcut) { |
| 38 | + AfterInterceptor tracingInterceptor = new AfterInterceptor(); |
| 39 | + DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor(); |
| 40 | + defaultPointcutAdvisor.setPointcut(myAspectJExpressionPointcut); |
| 41 | + defaultPointcutAdvisor.setAdvice(tracingInterceptor); |
| 42 | + return defaultPointcutAdvisor; |
| 43 | + } |
| 44 | + |
| 45 | + public static class TracingInterceptor implements MethodInterceptor { |
| 46 | + |
| 47 | + @Nullable |
| 48 | + @Override |
| 49 | + public Object invoke(@NotNull MethodInvocation invocation) throws Throwable { |
| 50 | + Method method = invocation.getMethod(); |
| 51 | + String name = method.getName(); |
| 52 | + System.out.println(name + "方法开始执行!"); |
| 53 | + Object proceed = invocation.proceed(); |
| 54 | + System.out.println(name + "方法执行结束!"); |
| 55 | + return proceed; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + public static class AfterInterceptor implements AfterReturningAdvice { |
| 62 | + @Override |
| 63 | + public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { |
| 64 | + System.out.println("这是一个后置切面"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | +} |
0 commit comments