AOP-前置原理-怎么判断和拦截?

判断模式

  • 类型(Class)
  • 方法(Method)
  • 注解 (Annotation)
  • 参数 (Parameter)
  • 异常 (Exception)
java 复制代码
public class TargetFilterDemo {
   public static void main(String[] args) throws ClassNotFoundException {
       String targetClassName = "com.yong.EchoService";
       Class<?> clazz = Class.forName(targetClassName);
       // 拦截类
       boolean assignableFrom = clazz.isAssignableFrom(EchoService.class);
       System.out.println(assignableFrom);
       // 通过方法名称和参数类型找到对应的方法
       Method method = ReflectionUtils.findMethod(clazz, "echo", String.class);
       System.out.println(method);
       // 过滤拦截方法 只对某个方法进行拦截
       ReflectionUtils.doWithMethods(clazz, (m) -> {
           System.out.println("i am target method! " + m.getName() + " paramCount " + m.getParameterCount());
       }, (m) -> {
           // 抛出RuntimeException异常的方法进行拦截
           Class<?>[] exceptionTypes = m.getExceptionTypes();
           if (exceptionTypes.length > 0 && RuntimeException.class == exceptionTypes[0]) {
               return true;
           }
           // 根据注解拦截
           Intercept[] annotation = m.getDeclaredAnnotationsByType(Intercept.class);
           return annotation != null && annotation.length > 0;
       });
   }
}

拦截模式

  • 前置拦截
  • 后置拦截
  • 异常拦截
    我们直接看例子,当拦截以后我们可以进行一系列操作,这只是初步了解一下怎么实现各种拦截,真实的AOP比这种方式更复杂,后文中会进行讨论。
java 复制代码
public class CustomInvocationHandler implements InvocationHandler {
    private final BeforeInterceptor beforeInterceptor;
    private final AfterInterceptor afterInterceptor;
    private final FinallyInterceptor finallyInterceptor;

    private final ExceptionInterceptor exceptionInterceptor;

    public CustomInvocationHandler(BeforeInterceptor beforeInterceptor, AfterInterceptor afterInterceptor, FinallyInterceptor finallyInterceptor, ExceptionInterceptor exceptionInterceptor) {
        this.beforeInterceptor = beforeInterceptor;
        this.afterInterceptor = afterInterceptor;
        this.finallyInterceptor = finallyInterceptor;
        this.exceptionInterceptor = exceptionInterceptor;
    }
	// 动态代理进行拦截
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 方法调用前
        if (beforeInterceptor != null) {
            beforeInterceptor.before(proxy, method, args);
        }
        Object result = null;
        try {
            EchoService echoService = new DefaultEchoService();
            result = method.invoke(echoService, args);
            // 方法调用后
            if (afterInterceptor != null) {
                afterInterceptor.after(proxy, method, args, result);
            }
           
        } catch (Exception e) {
            // 发生异常后
            if (exceptionInterceptor != null) {
                exceptionInterceptor.exception(proxy, method, args, e);
            }

        } finally {
            // finally
            if (finallyInterceptor != null) {
                finallyInterceptor.finallyExecute(proxy, method, args, result);
            }
        }
        return result;
    }
}
java 复制代码
public class DynamicProxyDemo {
    public static void main(String[] args) {
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        BeforeInterceptor beforeInterceptor = new DefaultBeforeInterceptor();
        AfterInterceptor afterInterceptor = new DefaultAfterInterceptor();
        FinallyInterceptor finallyInterceptor = new DefaultFinallyInterceptor();
        ExceptionInterceptor exceptionInterceptor = new DefaultExceptionInterceptor();
        EchoService proxyEchoService = (EchoService) Proxy.newProxyInstance(contextClassLoader, new Class[]{EchoService.class}, new CustomInvocationHandler(beforeInterceptor, afterInterceptor, finallyInterceptor, exceptionInterceptor));
        proxyEchoService.echo("hello World");
    }
}
相关推荐
天使day5 分钟前
SpringMVC
java·spring·java-ee
CodeClimb23 分钟前
【华为OD-E卷-简单的自动曝光 100分(python、java、c++、js、c)】
java·python·华为od
风清云淡_A33 分钟前
【java基础系列】实现数字的首位交换算法
java·算法
Gao_xu_sheng36 分钟前
Java程序打包成exe,无Java环境也能运行
java·开发语言
大卫小东(Sheldon)43 分钟前
Java的HTTP接口测试框架Gatling
java
谢家小布柔44 分钟前
java中的继承
java·开发语言
l138494274511 小时前
Java每日一题(2)
java·开发语言·游戏
苹果醋31 小时前
SpringBoot快速入门
java·运维·spring boot·mysql·nginx
WANGWUSAN661 小时前
Python高频写法总结!
java·linux·开发语言·数据库·经验分享·python·编程
Yvemil71 小时前
《开启微服务之旅:Spring Boot 从入门到实践》(一)
java