设为首页 加入收藏

TOP

Spring AOP自定义注解方式实现日志管理(二)
2015-11-12 23:05:54 来源: 作者: 【 】 浏览:39
Tags:Spring AOP 定义 注解 方式 实现 日志 管理
insert(SystemLog record) {
? ? ? ?
? ? ? ? return systemLogMapper.insertSelective(record);
? ? }


? ? @Override
? ? public SystemLog selectSystemLog(String id) {
? ? ? ?
? ? ? ? return systemLogMapper.selectByPrimaryKey(id);
? ? }


? ? @Override
? ? public int updateSystemLog(SystemLog record) {
? ? ? ?
? ? ? ? return systemLogMapper.updateByPrimaryKeySelective(record);
? ? }


? ? @Override
? ? public int insertTest(SystemLog record) {
? ? ? ?
? ? ? ? return systemLogMapper.insert(record);
? ? }


}


到这里基本程序编写完毕


下面开始自定义注解


package com.gcx.annotation;


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 "";
}


下面编写切面


package com.gcx.annotation;


import java.lang.reflect.Method;
import java.util.Date;
import java.util.UUID;


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 com.gcx.entity.SystemLog;
import com.gcx.entity.User;
import com.gcx.service.SystemLogService;
import com.gcx.util.JsonUtil;


/**
?* @author 杨建
?* @E-mail: email
?* @version 创建时间:2015-10-19 下午4:29:05
?* @desc 切点类
?*/


@Aspect
@Component
public class SystemLogAspect {


? ? //注入Service用于把日志保存数据库?
? ? @Resource? //这里我用resource注解,一般用的是@Autowired,他们的区别如有时间我会在后面的博客中来写
? ? private SystemLogService systemLogService;?
? ?
? ? private? static? final Logger logger = LoggerFactory.getLogger(SystemLogAspect. class);?
? ?
? ? //Controller层切点?
? ? @Pointcut("execution (* com.gcx.controller..*.*(..))")?
? ? public? void controllerAspect() {?
? ? }?
? ?
? ? /**
? ? * 前置通知 用于拦截Controller层记录用户的操作
? ? *
? ? * @param joinPoint 切点
? ? */
? ? @Before("controllerAspect()")
? ? public void doBefore(JoinPoint joinPoint) {
? ? ? ? System.out.println("==========执行controller前置通知===============");
? ? ? ? if(logger.isInfoEnabled()){
? ? ? ? ? ? logger.info("before " + joinPoint);
? ? ? ? }
? ? }? ?
? ?
? ? //配置controller环绕通知,使用在方法aspect()上注册的切入点
? ? ? @Around("controllerAspect()")
? ? ? public void around(JoinPoint joinPoint){
? ? ? ? ? System.out.println("==========开始执行controller环绕通知===============");
? ? ? ? ? long start = System.currentTimeMillis();
? ? ? ? ? try {
? ? ? ? ? ? ? ((ProceedingJoinPoint) joinPoint).proceed();
? ? ? ? ? ? ? long end = System.currentTimeMillis();
? ? ? ? ? ? ? if(logger.isInfoEnabled()){
? ? ? ? ? ? ? ? ? logger.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms!");
? ? ? ? ? ? ? }
? ? ? ? ? ? ? System.out.println("==========结束执行controller环绕通知===============");
? ? ? ? ? } catch (Throwable e) {
? ? ? ? ? ? ? long end = System.currentTimeMillis();
? ? ? ? ? ? ? if(logger.isInfoEnabled()){
? ? ? ? ? ? ? ? ? logger.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms with exception : " + e.getMes

首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Spring AOP的注解实例 下一篇Spring使用Cache

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: