aop注解方式实现全局日志管理方法

下面小编就为大家分享一篇aop注解方式实现全局日志管理方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

1:日志实体类

 public class SysLog { /** */ private Integer id; /** 日志描述*/ private String description; /** 执行的方法*/ private String method; /** 日志类型 0:操作日志;1:异常日志*/ private Integer logType; /** 客户端请求的ip地址*/ private String requestIp; /** 异常代码*/ private String exceptionCode; /** 异常详细信息*/ private String exceptionDetail; /** 请求参数*/ private String params; /** 操作人*/ private String createBy; /** 操作时间*/ private String createDate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public Integer getLogType() { return logType; } public void setLogType(Integer logType) { this.logType = logType; } public String getRequestIp() { return requestIp; } public void setRequestIp(String requestIp) { this.requestIp = requestIp; } public String getExceptionCode() { return exceptionCode; } public void setExceptionCode(String exceptionCode) { this.exceptionCode = exceptionCode; } public String getExceptionDetail() { return exceptionDetail; } public void setExceptionDetail(String exceptionDetail) { this.exceptionDetail = exceptionDetail; } public String getParams() { return params; } public void setParams(String params) { this.params = params; } public String getCreateBy() { return createBy; } public void setCreateBy(String createBy) { this.createBy = createBy; } public String getCreateDate() { return createDate; } public void setCreateDate(String createDate) { this.createDate = createDate; } }

2:maven需要的jar

  org.aspectjaspectjrt1.7.4 org.aspectjaspectjweaver1.7.4 cglibcglib2.1_3 org.springframeworkspring-aop4.2.5.RELEASE

这里要求项目使用的是jdk1.7

3:springServlet-mvc.xml

 

加上proxy-target-class="true"是为了可以拦截controller里面的方法

4:定义切面,我这里主要写前置通知和异常通知

下面开始自定义注解

 import java.lang.annotation.*; @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Log { /** 要执行的操作类型比如:add操作 **/ public String operationType() default ""; /** 要执行的具体操作比如:添加用户 **/ public String operationName() default ""; }

切面类

 import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; 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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.gtcity.user.model.SysLog; import com.gtcity.user.model.SysUser; import com.gtcity.user.service.SysLogService; /** * @author panliang * @version 创建时间:2017-3-31 * @desc 切点类 * */ @Aspect @Component public class SystemLogAspect { //注入Service用于把日志保存数据库 @Resource private SysLogService systemLogService; private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect. class); //Controller层切点 //第一个*代表所有的返回值类型 //第二个*代表所有的类 //第三个*代表类所有方法 //最后一个..代表所有的参数。 @Pointcut("execution (* com.gtcity.web.controller..*.*(..))") public void controllerAspect() { } /** * * @author: panliang * @time:2017-3-31 下午2:22:16 * @param joinPoint 切点 * @describtion:前置通知 用于拦截Controller层记录用户的操作 */ @Before("controllerAspect()") public void doBefore(JoinPoint joinPoint) { /* System.out.println("==========执行contro

以上就是aop注解方式实现全局日志管理方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java