jdk动态代理,异常处理逻辑优化。
整体异常流程如下: 1、末端拦截器处理,若未能捕获,则继续流转 2、全局拦截器处理,若未能捕获、则继续流转 3、若被代理方法声明了对外抛出,则抛给应用层,否则继续流转 4、代理方法处理,直接打印堆栈信息
This commit is contained in:
+5
-1
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-4
@@ -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<Class<?>> exceptionTypes = ReflectUtil.getExceptionTypes(method);
|
||||
if (CollectionUtil.notEmpty(exceptionTypes) && exceptionTypes.contains(e.getClass())) {
|
||||
throw e;
|
||||
} else {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return inv.getReturnValue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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("跳太高,摔死了!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,10 @@ public class Test1 implements ApplicationRunner {
|
||||
user2.setEmail("[email protected]");
|
||||
user2.setSex("男");
|
||||
user2.setCreateTime(new Date());
|
||||
System.out.println(userMapper.add(user2));
|
||||
try {
|
||||
System.out.println(userMapper.add(user2));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
+6
-1
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user