mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
1、切面after切点,将目标方法执行返回值,开放给切点
2、SimpleAspect没有直接使用的意义,应修改为抽象类 3、修改Aspect#afterException 没有使用返回值进行异常是否抛出的判断。 4、修改CglibInterceptor#intercept 和 JdkInterceptor#intercept 方法中 if else 为 if ,减少else判断,并支持 第3点的修改
This commit is contained in:
parent
130a348afc
commit
22a51ba86d
@ -23,7 +23,11 @@ public interface Aspect{
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 目标方法执行后的操作
|
* 目标方法执行后的操作
|
||||||
*
|
* 如果 target.method 抛出异常且
|
||||||
|
* @see Aspect#afterException 返回true,则不会执行此操作
|
||||||
|
* 如果
|
||||||
|
* @see Aspect#afterException 返回false,则无论target.method是否抛出异常,均会执行此操作
|
||||||
|
*
|
||||||
* @param target 目标对象
|
* @param target 目标对象
|
||||||
* @param method 目标方法
|
* @param method 目标方法
|
||||||
* @param args 参数
|
* @param args 参数
|
||||||
|
@ -14,18 +14,30 @@ import java.lang.reflect.Method;
|
|||||||
public abstract class SimpleAspect implements Aspect, Serializable{
|
public abstract class SimpleAspect implements Aspect, Serializable{
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see Aspect#before(Object, Method, Object[])
|
||||||
|
* @return 是否继续执行接下来的操作 默认值true
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean before(Object target, Method method, Object[] args) {
|
public boolean before(Object target, Method method, Object[] args) {
|
||||||
//继承此类后实现此方法
|
//继承此类后实现此方法
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see Aspect#after(Object, Method, Object[], Object)
|
||||||
|
* @return 是否允许返回值(接下来的操作) 默认值true
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean after(Object target, Method method, Object[] args, Object returnVal) {
|
public boolean after(Object target, Method method, Object[] args, Object returnVal) {
|
||||||
//继承此类后实现此方法
|
//继承此类后实现此方法
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see Aspect#afterException(Object, Method, Object[], Throwable)
|
||||||
|
* @return 是否允许抛出异常 默认值true
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean afterException(Object target, Method method, Object[] args, Throwable e) {
|
public boolean afterException(Object target, Method method, Object[] args, Throwable e) {
|
||||||
//继承此类后实现此方法
|
//继承此类后实现此方法
|
||||||
|
@ -46,11 +46,14 @@ public class CglibInterceptor implements MethodInterceptor, Serializable {
|
|||||||
result = proxy.invokeSuper(obj, args);
|
result = proxy.invokeSuper(obj, args);
|
||||||
} catch (UtilException e) {
|
} catch (UtilException e) {
|
||||||
final Throwable cause = e.getCause();
|
final Throwable cause = e.getCause();
|
||||||
if (e.getCause() instanceof InvocationTargetException) {
|
if (!(e.getCause() instanceof InvocationTargetException)) {
|
||||||
aspect.afterException(target, method, args, ((InvocationTargetException) cause).getTargetException());
|
// 其它异常属于代理的异常,直接抛出
|
||||||
} else {
|
throw e;
|
||||||
throw e;// 其它异常属于代理的异常,直接抛出
|
|
||||||
}
|
}
|
||||||
|
if(aspect.afterException(target, method, args, ((InvocationTargetException) cause).getTargetException())){
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (aspect.after(target, method, args, result)) {
|
if (aspect.after(target, method, args, result)) {
|
||||||
|
@ -47,10 +47,12 @@ public class JdkInterceptor implements InvocationHandler, Serializable{
|
|||||||
result = ReflectUtil.invoke(target, method, args);
|
result = ReflectUtil.invoke(target, method, args);
|
||||||
} catch (UtilException e) {
|
} catch (UtilException e) {
|
||||||
final Throwable cause = e.getCause();
|
final Throwable cause = e.getCause();
|
||||||
if (e.getCause() instanceof InvocationTargetException) {
|
if (!(e.getCause() instanceof InvocationTargetException)) {
|
||||||
aspect.afterException(target, method, args, ((InvocationTargetException) cause).getTargetException());
|
// 其它异常属于代理的异常,直接抛出
|
||||||
} else {
|
throw e;
|
||||||
throw e;// 其它异常属于代理的异常,直接抛出
|
}
|
||||||
|
if(aspect.afterException(target, method, args, ((InvocationTargetException) cause).getTargetException())){
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user