转载: http://www.cnblogs.com/guokai870510826/p/5981015.html

使用标签来设置需要的记录

实例:@ISystemLog()

@Controller
@RequestMapping("test")
public class TestController {
@RequestMapping(value = "test.do", method = RequestMethod.GET)
@ISystemLog(module = "TestController",methods = "test")
public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest request1 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
return new ModelAndView("/login");
}
}

创建ISystemLog

package com.helka.cmis.common.utils.log;

import java.lang.annotation.*;
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ISystemLog {
String module() default "";
String methods() default "";
}

创建LogAopAction

package com.helka.cmis.common.utils.log;

import com.helka.cmis.common.utils.LogEntity;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
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.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date; @Aspect
public class LogAopAction { // @Resource(name="logService")
// private LogServiceImpl logservice; //配置接入点,如果不知道怎么配置,可以百度一下规则
@Pointcut("execution(* com.kintech.*.controller..*.*(..))")
private void controllerAspect(){}
// @Autowired
// private HttpServletRequest request; //@Around("execution(* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(..))")
@Around("controllerAspect()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
//常见日志实体对象
LogEntity log = new LogEntity();
//获取登录用户账户
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); ServletWebRequest servletWebRequest=new ServletWebRequest(request);
HttpServletResponse response=servletWebRequest.getResponse(); String name = (String) request.getSession().getAttribute("USER_ID");
log.setAccount(name);
//获取系统时间
String time = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date());
log.setCreatetime(time); String ip = request.getHeader("X-Real-IP");
log.setIp(ip); long start = System.currentTimeMillis();
// 拦截的实体类,就是当前正在执行的controller
Object target = pjp.getTarget();
// 拦截的方法名称。当前正在执行的方法
String methodName = pjp.getSignature().getName();
// 拦截的方法参数
Object[] args = pjp.getArgs();
// 拦截的放参数类型
Signature sig = pjp.getSignature();
MethodSignature msig = null;
if (!(sig instanceof MethodSignature)) {
throw new IllegalArgumentException("该注解只能用于方法");
}
msig = (MethodSignature) sig;
Class[] parameterTypes = msig.getMethod().getParameterTypes(); Object object = null;
// 获得被拦截的方法
Method method = null;
try {
method = target.getClass().getMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SecurityException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (null != method) {
// 判断是否包含自定义的注解,说明一下这里的SystemLog就是我自己自定义的注解
if (method.isAnnotationPresent(ISystemLog.class)) {
ISystemLog systemlog = method.getAnnotation(ISystemLog.class);
log.setModule(systemlog.module());
log.setMethod(systemlog.methods());
try {
object = pjp.proceed();
long end = System.currentTimeMillis();
//将计算好的时间保存在实体中
log.setUsedtime(end-start);
log.setCommit("Success!");
//保存进数据库
//logservice.saveLog(log);
} catch (Throwable e) {
// TODO Auto-generated catch block
long end = System.currentTimeMillis();
log.setUsedtime(end-start);
log.setCommit("Failed");
//logservice.saveLog(log);
}
} else {//没有包含注解
object = pjp.proceed();
}
} else { //不需要拦截直接执行
object = pjp.proceed();
}
return object;
}
}

创建数据库的映射实体

public class LogEntity {

    private int id;
private String account;
private String module;
private String method;
private long usedtime;
private String ip;
private String createtime;
private String commit; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getAccount() {
return account;
} public void setAccount(String account) {
this.account = account;
} public String getModule() {
return module;
} public void setModule(String module) {
this.module = module;
} public String getMethod() {
return method;
} public void setMethod(String method) {
this.method = method;
} public long getUsedtime() {
return usedtime;
} public void setUsedtime(long usedtime) {
this.usedtime = usedtime;
} public String getIp() {
return ip;
} public void setIp(String ip) {
this.ip = ip;
} public String getCreatetime() {
return createtime;
} public void setCreatetime(String createtime) {
this.createtime = createtime;
} public String getCommit() {
return commit;
} public void setCommit(String commit) {
this.commit = commit;
} }

配置文件:

springmvc-servlet.xml

<aop:aspectj-autoproxy proxy-target-class="true" />
<bean id="logAopAction" class="com.helka.cmis.common.utils.log.LogAopAction" />

最新文章

  1. Cocoapods的安装,卸载和使用
  2. 解决webApi&lt;Message&gt;An error has occurred.&lt;/Message&gt;不能写多个Get方法的问题
  3. PPT图片快速编辑技巧
  4. codevs 1378 选课 (树形DP)
  5. 洛谷 P3367 【模板】并查集
  6. LOOPS
  7. C# new和override的区别
  8. 如何给 UILable 添加横线
  9. 关于php调用.net的web service 踩过的坑
  10. background背景透明内容不透明
  11. sklearn 划分数据集。
  12. JS跨域两三事
  13. 闭区间套定理(Nested intervals theorem)讲解2
  14. 享元(FlyWeight)模式
  15. ***实用函数:PHP explode()函数用法、切分字符串,作用,将字符串打散成数组
  16. 在Visual Studio中使用类图描述领域模型
  17. 《Netty权威指南》
  18. SpringMVC -- 梗概--源码--壹--springMVC json处理
  19. Jersey入门一:从Maven Archetype创建jersey项目
  20. SecureCRT 快捷键总结 设置快捷键

热门文章

  1. img图片加载出错处理(转载)
  2. WPF模拟探照灯文字
  3. .NET小笔记-NPOI读取excel内容到DataTable
  4. Spring Aop: 关于继承和execution target this @annotation
  5. hadoop_spark伪分布式实验环境搭建和运行实例详细教程
  6. Python从菜鸟到高手(2):清空Python控制台
  7. Beta版本发布报告
  8. Linux内核及分析 第八周 进程的切换和系统的一般执行过程
  9. text3
  10. 第三个Sprint冲刺第5天