欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > AOP-前置原理-怎么判断和拦截?

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

2025/2/6 9:18:39 来源:https://blog.csdn.net/qq_43259860/article/details/142389443  浏览:    关键词:AOP-前置原理-怎么判断和拦截?

判断模式

  • 类型(Class)
  • 方法(Method)
  • 注解 (Annotation)
  • 参数 (Parameter)
  • 异常 (Exception)
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比这种方式更复杂,后文中会进行讨论。
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;}// 动态代理进行拦截@Overridepublic 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 {// finallyif (finallyInterceptor != null) {finallyInterceptor.finallyExecute(proxy, method, args, result);}}return result;}
}
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");}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com