注解自己定义即可,支持方法
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement) args[0];
MyAnnotation an= null;
for(Method m : loadMethod(ms.getId())) {
an= m.getDeclaredAnnotation(MyAnnotation.class);
if(null != an) {
break;
}
}
if (null == an) {
logger.debug("==>非注解方法");
return invocation.proceed();
}
loadMethod获取方法,原理是MappedStatement 的id是类全路径+方法名,截取类和方法,反射得到。使用list解决重写的方法问题,避免拿不到父类的方法。
public List<Method> loadMethod(final String id) {
List<Method> list = new ArrayList<>();
try {
String className = id.substring(0, id.lastIndexOf("."));
String methodName = id.substring(id.lastIndexOf(".") + 1);
Class<?> cls = Class.forName(className);
for (Method m : cls.getDeclaredMethods()) {
if (methodName.equalsIgnoreCase(m.getName())) {
list.add(m);
}
}
} catch (Exception e) {
}
return list;
}