話不多說直接上代碼,不喜勿噴
@After("@annotation(com.你的路徑.service.LogAntn)")
public void afterAdvice(JoinPoint joinPoint) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException{
Object[] args = joinPoint.getArgs();
User user = null;
Map<String, String> paramsMap = null;
for (Object object : args) {
if(object instanceof User){
user = (User)object;
}else if(object instanceof Map){
paramsMap = (Map<String, String>)object;
}
}
LogAntn logAntn = getAntn(joinPoint);
String signature = joinPoint.getSignature().toString(); // 獲取目標方法簽名
String methodName = signature.substring(signature.lastIndexOf(".") + 1,signature.indexOf("("));
String clazzName = joinPoint.getTarget().getClass().getName();
Class<?> clazz = Class.forName(clazzName);
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(LogAntn.class)&& method.getName().equals(methodName)) {
opObj opObjType = logAntn.opObjType();
String objVal = opObjType.getValue();//操作對象
opType type = logAntn.type();
String opVal = type.getValue();//操作類型
//添加入庫操作
}
}
logger.info("aop log ... this is after Advice......");
}
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAntn {
enum opType{
// UPDATE,ADD,DELETE,REPORT,SEND,TURNEDDOWN
UPDATE("UPDATE","修改/維護"),
DEL("DEL","刪除/撤銷"),
ADD("ADD","添加");
private String key;
private String value;
private opType(String key,String value) {
this.key = key;
this.value = value;
}
public String getKey(String key){
return key;
}
public String getValue() {
return value;
}
};
opType type();
enum opObj{//操作的簡單描述
REPORT("REPORT","上報審批");
};
opObj opObjType();
}
//獲取註解
public static LogAntn getAntn(JoinPoint joinPoint){
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null) {
return method.getAnnotation(LogAntn.class);
}
return null;
}