From 4911df99c3a1658275305ada6e23476485c701b4 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Thu, 30 Jun 2022 16:06:13 +0800 Subject: [PATCH] =?UTF-8?q?jdk=E5=8A=A8=E6=80=81=E4=BB=A3=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E3=80=82=20=E6=95=B4=E4=BD=93=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E6=B5=81=E7=A8=8B=E5=A6=82=E4=B8=8B=EF=BC=9A=201?= =?UTF-8?q?=E3=80=81=E6=9C=AB=E7=AB=AF=E6=8B=A6=E6=88=AA=E5=99=A8=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E8=8B=A5=E6=9C=AA=E8=83=BD=E6=8D=95=E8=8E=B7?= =?UTF-8?q?=EF=BC=8C=E5=88=99=E7=BB=A7=E7=BB=AD=E6=B5=81=E8=BD=AC=202?= =?UTF-8?q?=E3=80=81=E5=85=A8=E5=B1=80=E6=8B=A6=E6=88=AA=E5=99=A8=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E8=8B=A5=E6=9C=AA=E8=83=BD=E6=8D=95=E8=8E=B7?= =?UTF-8?q?=E3=80=81=E5=88=99=E7=BB=A7=E7=BB=AD=E6=B5=81=E8=BD=AC=203?= =?UTF-8?q?=E3=80=81=E8=8B=A5=E8=A2=AB=E4=BB=A3=E7=90=86=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=A3=B0=E6=98=8E=E4=BA=86=E5=AF=B9=E5=A4=96=E6=8A=9B=E5=87=BA?= =?UTF-8?q?=EF=BC=8C=E5=88=99=E6=8A=9B=E7=BB=99=E5=BA=94=E7=94=A8=E5=B1=82?= =?UTF-8?q?=EF=BC=8C=E5=90=A6=E5=88=99=E7=BB=A7=E7=BB=AD=E6=B5=81=E8=BD=AC?= =?UTF-8?q?=204=E3=80=81=E4=BB=A3=E7=90=86=E6=96=B9=E6=B3=95=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E7=9B=B4=E6=8E=A5=E6=89=93=E5=8D=B0=E5=A0=86?= =?UTF-8?q?=E6=A0=88=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aop/interceptor/InnerGlobalInterceptor.java | 6 +++++- .../core/aop/interceptor/InterceptorWrapper.java | 3 ++- .../core/aop/proxy/JdkDynamicProxyFactory.java | 16 ++++++++++++---- .../java/fun/asgc/neutrino/core/aop/Panda.java | 4 ++++ .../java/fun/asgc/neutrino/core/aop/Test2.java | 10 ++++++++++ .../fun/asgc/neutrino/core/db/mapper/Test1.java | 6 +++++- .../core/db/mapper/TestExceptionHandler.java | 5 +++++ .../db/mapper/TestGlobalExceptionHandler.java | 7 ++++++- .../asgc/neutrino/core/db/mapper/UserMapper.java | 2 +- 9 files changed, 50 insertions(+), 9 deletions(-) diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InnerGlobalInterceptor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InnerGlobalInterceptor.java index 9a4cf798..f3844a64 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InnerGlobalInterceptor.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InnerGlobalInterceptor.java @@ -42,8 +42,12 @@ public class InnerGlobalInterceptor implements Interceptor { */ private static final InterceptorWrapper interceptorWrapper = new InterceptorWrapper(InnerGlobalInterceptor.class.getSimpleName()); + public InnerGlobalInterceptor() { + System.out.println("aaaaa"); + } + @Override - public void intercept(Invocation inv) { + public void intercept(Invocation inv) throws Exception { interceptorWrapper.intercept(inv); } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorWrapper.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorWrapper.java index 6533c96e..8dde79c4 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorWrapper.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/interceptor/InterceptorWrapper.java @@ -51,7 +51,7 @@ public class InterceptorWrapper implements Interceptor { } @Override - public void intercept(Invocation inv) { + public void intercept(Invocation inv) throws Exception { try { log.debug("拦截器:{} class:{} method:{} args:{} before", this.name, inv.getTargetClass().getName(), inv.getTargetMethod().getName(), inv.getArgs()); if (CollectionUtil.notEmpty(filterList)) { @@ -89,6 +89,7 @@ public class InterceptorWrapper implements Interceptor { } } } + throw e; } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/proxy/JdkDynamicProxyFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/proxy/JdkDynamicProxyFactory.java index 629d4bda..f01cc922 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/proxy/JdkDynamicProxyFactory.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/proxy/JdkDynamicProxyFactory.java @@ -22,13 +22,12 @@ package fun.asgc.neutrino.core.aop.proxy; import fun.asgc.neutrino.core.aop.Invocation; -import fun.asgc.neutrino.core.util.ArrayUtil; -import fun.asgc.neutrino.core.util.Assert; -import fun.asgc.neutrino.core.util.ClassUtil; +import fun.asgc.neutrino.core.util.*; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; +import java.util.Set; /** * JDK动态代理 @@ -82,7 +81,16 @@ public class JdkDynamicProxyFactory implements ProxyFactory { } return null; }, args); - inv.invoke(); + try { + inv.invoke(); + } catch (Exception e) { + Set> exceptionTypes = ReflectUtil.getExceptionTypes(method); + if (CollectionUtil.notEmpty(exceptionTypes) && exceptionTypes.contains(e.getClass())) { + throw e; + } else { + e.printStackTrace(); + } + } return inv.getReturnValue(); } } diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Panda.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Panda.java index 6e5e39fc..558699dc 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Panda.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Panda.java @@ -54,4 +54,8 @@ public class Panda { public String request(String a, String b) { return String.format("参数 a:%s b:%s 处理结果", a, b); } + + public void up() throws Exception { + throw new Exception("跳太高,摔死了!"); + } } diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Test2.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Test2.java index 4fff870e..bee9a392 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Test2.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/Test2.java @@ -107,4 +107,14 @@ public class Test2 { Panda panda = Aop.get(Panda.class); panda.request("xxx", "yyy"); } + + @Test + public void up() { + Panda panda = Aop.get(Panda.class); + try { + panda.up(); + } catch (Exception e) { + e.printStackTrace(); + } + } } diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/Test1.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/Test1.java index fe488c07..5dd04d93 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/Test1.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/Test1.java @@ -59,6 +59,10 @@ public class Test1 implements ApplicationRunner { user2.setEmail("liba@qq.com"); user2.setSex("男"); user2.setCreateTime(new Date()); - System.out.println(userMapper.add(user2)); + try { + System.out.println(userMapper.add(user2)); + } catch (Exception e) { + e.printStackTrace(); + } } } diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/TestExceptionHandler.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/TestExceptionHandler.java index 9a5df850..69713d5e 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/TestExceptionHandler.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/TestExceptionHandler.java @@ -25,6 +25,11 @@ import java.sql.SQLException; @Slf4j public class TestExceptionHandler implements ExceptionHandler { + /** + * 此处若拦截成功,则全局异常拦截不会执行 + * @param e + * @return + */ @Override public boolean support(Exception e) { // return e instanceof SQLException; diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/TestGlobalExceptionHandler.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/TestGlobalExceptionHandler.java index 5064e975..1d2ac469 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/TestGlobalExceptionHandler.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/TestGlobalExceptionHandler.java @@ -25,9 +25,14 @@ import lombok.extern.slf4j.Slf4j; @Component public class TestGlobalExceptionHandler implements ExceptionHandler { + /** + * + * @param e + * @return + */ @Override public boolean support(Exception e) { - return true; + return false; } @Override diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/UserMapper.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/UserMapper.java index f8f816c6..40f67485 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/UserMapper.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/db/mapper/UserMapper.java @@ -49,5 +49,5 @@ public interface UserMapper extends SqlMapper { int count(); @Insert("insert into user(`id`,`name`,`age`,`email`,`sex`,`create_time`) values(:id,:name,:age,:email,:sex,:createTime)") - int add(User user); + int add(User user) throws Exception; }