1.自定义注解SysLog
package com.zhong.spring_demo.anno;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 定义系统日志注解
*
* @author zhong
* @date 2018/6/4 9:24
* @email 126@qq.com
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
String value() default "";
}
2. 编写切面SysLogAspect类
package com.zhong.spring_demo.aspect;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.google.gson.Gson;
import com.zhong.spring_demo.anno.SysLog;
import com.zhong.spring_demo.bo.SysLogBO;
import com.zhong.spring_demo.service.SysLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 系统日志切面
*
* @author zhong
*/
@Aspect // 使用@Aspect注解声明一个切面
@Component
public class SysLogAspect {
@Autowired
private SysLogService sysLogService;
/**
* 这里我们使用注解的形式 当然,我们也可以通过切点表达式直接指定需要拦截的package,需要拦截的class 以及 method 切点表达式:
* execution(...)
*/
@Pointcut("@annotation(com.zhong.spring_demo.anno.SysLog)")
public void logPointCut() {
}
/**
* 环绕通知 @Around , 当然也可以使用 @Before (前置通知) @After (后置通知)
*
* @param point
* @return
* @throws Throwable
*/
@Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
long beginTime = System.currentTimeMillis();
Object result = point.proceed();
long time = System.currentTimeMillis() - beginTime;
try {
saveLog(point, time);
} catch (Exception e) {
}
return result;
}
/**
* 保存日志
*
* @param joinPoint
* @param time
*/
private void saveLog(ProceedingJoinPoint joinPoint, long time) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
SysLogBO sysLogBO = new SysLogBO();
sysLogBO.setExeuTime(time);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
sysLogBO.setCreateDate(dateFormat.format(new Date()));
SysLog sysLog = method.getAnnotation(SysLog.class);
if (sysLog != null) {
// 注解上的描述
sysLogBO.setRemark(sysLog.value());
}
// 请求的 类名、方法名
String className = joinPoint.getTarget().getClass().getName();
String methodName = signature.getName();
sysLogBO.setClassName(className);
sysLogBO.setMethodName(methodName);
// 请求的参数
Object[] args = joinPoint.getArgs();
try {
List<String> list = new ArrayList<String>();
for (Object o : args) {
list.add(new Gson().toJson(o));
}
sysLogBO.setParams(list.toString());
} catch (Exception e) {
}
sysLogService.save(sysLogBO);
}
}
3.测试使用自定义注解
package com.zhong.spring_demo.controller;
import com.zhong.spring_demo.anno.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* TestController
*/
@RestController
public class TestController {
@SysLog("测试")
@GetMapping("/test")
public String test(@RequestParam("name") String name){
return name;
}
}