切面配置,实现自定义注解
package com.micvs.modular.system.annotation;
import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogValidate{
String module() default "";
}
配置加载
package com.micvs.modular.system.aspect;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.micvs.core.shiro.ShiroKit;
import com.micvs.core.util.RestService;
import com.micvs.core.util.UUIDUtils;
import com.micvs.modular.system.annotation.LogValidate;
import com.micvs.modular.system.entity.AopOperationLog;
import com.micvs.modular.system.service.OperationLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
@Aspect
@Component
public class SerurAspect {
@Resource
private OperationLogService operationLogService;
private static final Logger logger = LoggerFactory.getLogger(RestService.class);
@Pointcut("@annotation(com.micvs.modular.system.annotation.LogValidate)")
public void login() {
}
@Before("login()")
public void begin() {
}
@AfterReturning("login()")
public void afterReturning() {
}
@After("login()")
public void after() {
}
@AfterThrowing("login()")
public void afterThrowing() {
}
@Around("login()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
LogValidate annotation = method.getAnnotation(LogValidate.class);
String methodName = joinPoint.getSignature().getName();
Object[] params = joinPoint.getArgs();
if (!StringUtils.isEmpty(methodName)){
Date date=new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
AopOperationLog parm=new AopOperationLog();
parm.setId(UUIDUtils.getUUID());
parm.setCreatTime(format.format(date));
parm.setMethod(methodName);
parm.setOperationName(ShiroKit.getUser().getName());
if (annotation != null) {
parm.setOperationType(annotation.module());
}
if (params != null) {
parm.setParm(JSON.toJSONString(params));
}
String msg=operationLogService.saveOperationLog(parm);
logger.info(JSONObject.toJSONString(parm));
logger.info(JSONObject.toJSONString(msg));
}
return joinPoint.proceed();
}
}
切面引用
@LogValidate(module = "标题")
方法体 void(){
?????
}