使用Aspect切面实现系统日志并存入数据库
1.pom.xml中:加入Maven依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
/dependency>
2.SpringMVC.xml中:开启切面注解
<aop:aspectj-autoproxy proxy-target-class="true" />
3.自定义注解用于切面切入点
import java.lang.annotation.*;
/**
* 系统日志注解
*
* @author wangxueqing
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
String value() default "";
}
4.自定义切面在方法执行时自动执行
import com.alibaba.fastjson.JSON;
import com.sm.share3d.annotion.SysLog;
import com.sm.share3d.bean.SysLogEntity;
import com.sm.share3d.utils.HttpContextUtils;
import com.sm.share3d.utils.IPUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 系统日志,切面处理类
*
* @author wangxueqing
*/
@Component
@Aspect
public class SysLogAspect {
//切入点,以方法的形式存在
@Pointcut("@annotation(com.sm.share3d.annotion.SysLog)")
public void sportPoint(){
System.out.println(1111111);
}
@Before("sportPoint()")
public void before(JoinPoint joinPoint){
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
SysLogEntity logEntity = new SysLogEntity();
SysLog syslog = method.getAnnotation(SysLog.class);
if(syslog != null){
//注解上的描述
logEntity.setOperation(syslog.value());
}
//请求的方法名
String className = joinPoint.getTarget().getClass().getName();
String methodName = signature.getName();
logEntity.setMethod(className + "." + methodName + "()");
//请求的参数
Object[] args = joinPoint.getArgs();
if(args.length>0){
/*String params = JSON.toJSONString(args[0]);
logEntity.setParams(params);*/
}
//获取request
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
//设置IP地址
logEntity.setIp(IPUtils.getIpAddr(request));
//用户名
// String username = ShiroUtils.getUserEntity().getUsername();
logEntity.setUsername(username);
logEntity.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
sysLogService.save(logEntity);
}
}
5.在Controller层需要保存日志的方法上加上自定义的注解
/**
* 保存用户
* @param user
* @return
*/
@SysLog("新增用户")
@RequestMapping("/saveUser")
public R saveUser(@RequestBody User user){
try {
userService.save(user);
return R.ok();
} catch (Exception e) {
e.printStackTrace();
return R.error("新增失败");
}
}