操作日志具体实现内容
下面看看它的实现步骤:
- 首先创建一个操作日志注解类
/**
* 操作日志
*/
@Target({
ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EmployeeLog {
/**模块*/
String module() default "";
/**描述*/
String description() default "";
}
- 然后创建一个切面日志处理类
/**
* 日志切面处理类
*/
@Aspect
@Component
public class EmployeeLogAspect {
@Autowired
private EmployeeLogServiceImpl employeeLogServiceImpl;
/**
* 日志切入点
*/
@Pointcut("@annotation(com.wp.annotation.EmployeeLog)")
//@Pointcut("execution(* com.wp.service.impl.EmployeeManageServiceImpl.*(..))")
public void logPointCut(){
}
@AfterReturning(pointcut = "logPointCut()")
public void doAfter(JoinPoint joinPoint){
/**
* 解析Log注解
*/
String methodName = joinPoint.getSignature().getName();
Method method = currentMethod(joinPoint,methodName);
EmployeeLog log = method.getAnnotation(EmployeeLog.class);
employeeLogServiceImpl.put(joinPoint,methodName,log.module(),log.description());
}
/**
* 获取当前执行的方法
*
* @param joinPoint 连接点
* @param methodName 方法名称
* @return 方法
*/
private Method currentMethod(JoinPoint joinPoint, String methodName) {
/**
* 获取目标类的所有方法,找到当前要执行的方法
*/
Method[] methods = joinPoint.getTarget().getClass().getMethods();
Method resultMethod = null;
for (Method method : methods) {
if (method.getName().equals(methodName)) {
resultMethod = method;
break;
}
}
return resultMethod;