springmvc自定义注解实现日志记录

1.自定义注解

package com.lawschool.annotation;

import java.lang.annotation.*;

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface SysLog {

String value() default "";

}

2.切面

package com.lawschool.annotation;

import com.alibaba.fastjson.JSON;

import com.lawschool.beans.SysLogEntity;

import com.lawschool.beans.User;

import com.lawschool.service.SysLogService;

import com.lawschool.util.GetUUID;

import com.lawschool.util.HttpContextUtils;

import com.lawschool.util.IpUtil;

import com.lawschool.util.UtilValidate;

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;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;

import java.lang.reflect.Method;

import java.util.Date;

@Aspect

@Component

public class SysLogAspect extends HandlerInterceptorAdapter {

@Autowired

private SysLogService sysLogService;

@Pointcut("@annotation(com.lawschool.annotation.SysLog)")

public void logPointCut() {

}

@Around("logPointCut()")

public Object around(ProceedingJoinPoint point) throws Throwable {

long beginTime = System.currentTimeMillis();

//执行方法

Object result = point.proceed();

//执行时长(毫秒)

long time = System.currentTimeMillis() - beginTime;

//保存日志

saveSysLog(point, time);

return result;

}

private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {

MethodSignature signature = (MethodSignature) joinPoint.getSignature();

Method method = signature.getMethod();

SysLogEntity sysLog = new SysLogEntity();

sysLog.setId(GetUUID.getUUIDs("LO"));//

SysLog syslog = method.getAnnotation(SysLog.class);

if(syslog != null){

//注解上的描述

sysLog.setOperation(syslog.value());

}

//请求的方法名

String className = joinPoint.getTarget().getClass().getName();

String methodName = signature.getName();

sysLog.setMethod(className + "." + methodName + "()");

//请求的参数

Object[] args = joinPoint.getArgs();

try{

String params = JSON.toJSONString(args[0]);

sysLog.setParams(params);

}catch (Exception e){

}

//获取request

HttpServletRequest request = HttpContextUtils.getHttpServletRequest();

//设置IP地址

sysLog.setIp(IpUtil.getIpAddr(request));

//用户名

User user=(User)request.getSession().getAttribute("user");

String username ="";

if(UtilValidate.isNotEmpty(user)){

username =user.getUserName();

}

sysLog.setUsername(username);

sysLog.setTime(time);

sysLog.setCreateDate(new Date());

//保存系统日志

sysLogService.insert(sysLog);

}

}

3.相关的配置文件的配置

<aop:aspectj-autoproxy proxy-target-class="true"/>

<mvc:annotation-driven />

<!-- 扫描annotation(annotation层注入) -->

<context:component-scan base-package="com.lawschool.annotation">

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />

</context:component-scan>

<!-- Log自定义注解拦截器 -->

<mvc:interceptors>

<mvc:interceptor>

<mvc:mapping path="/**"/>

<bean class="com.lawschool.annotation.SysLogAspect"></bean>

</mvc:interceptor>

</mvc:interceptors>

4.相关的pom文件引入jar

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>3.0.6.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aspects</artifactId>

<version>3.0.6.RELEASE</version>

</dependency>

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjrt</artifactId>

<version>1.6.11</version>

</dependency>

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.6.11</version>

</dependency>

5.相关的controller添加注解

/**

* @Author MengyuWu

* @Description 添加参数配置

* @Date 17:45 2018-12-7

* @Param [config]

* @return com.lawschool.util.Result

**/

@SysLog("添加参数配置")

@RequestMapping("/insert")

public Result insert(@RequestBody SysConfig config){

config.setId(GetUUID.getUUIDs("SC"));

configService.insert(config);

return Result.ok().put("id",config.getId());

}

springmvc自定义注解实现日志记录

相关推荐