一个基于spring aop实现的日志系统


import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.tc.common.model.BusinessSystemOperationLog;
import com.tc.core.annotation.SystemControllerLog;
import com.tc.service.system.OperationLogService;

@Aspect   
@Component
public class SystemLogAspect {

@Autowired
private OperationLogService operationLogService;
//本地异常日志记录对象   
    private  static  final Logger logger = LoggerFactory.getLogger(SystemLogAspect. class);   
  

    //Service层切点   
    @Pointcut("@annotation(com.tc.core.annotation.SystemServiceLog)")   
    public  void serviceAspect() {
   
    }   
   
   
  //Controller层切点   
    @Pointcut("@annotation(com.tc.core.annotation.SystemControllerLog)")   
    public  void controllerAspect() {  
   
    } 
   
    /** 
     * 前置通知 用于拦截Controller层记录用户的操作 
     * 
     * @param joinPoint 切点 
     */   
    @Before("controllerAspect()")   
     public  void doBefore(JoinPoint joinPoint) {   
   
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();   
        HttpSession session = request.getSession();   
        //读取session中的用户   
//        User user = (User) session.getAttribute(WebConstants.CURRENT_USER);   
        //请求的IP   
        String ip = request.getRemoteAddr();   
         try {   
            //*========控制台输出=========*//   
        logger.info("=====日志记录开始=====");   
        String content = getControllerMethodDescription(joinPoint);
        logger.info("请求方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));   
        logger.info("方法描述:" + content);   
//            System.out.println("请求人:" + user.getName());   
        logger.info("请求IP:" + ip);   
            //*========数据库日志=========*//   
            BusinessSystemOperationLog log =  new BusinessSystemOperationLog();
            log.setContent(content);   
            log.setIpAddress(ip);
            //保存数据库   
            operationLogService.insertSystemLog(log);
            System.out.println("=====日志记录结束=====");   
        }  catch (Exception e) {   
        e.printStackTrace();
            //记录本地异常日志   
            logger.error("==日志记录异常==");   
            logger.error("异常信息:{}", e.getMessage());   
        }   
    }
   
    /** 
     * 获取注解中对方法的描述信息 用于Controller层注解 
     * 
     * @param joinPoint 切点 
     * @return 方法描述 
     * @throws Exception 
     */   
     public  static String getControllerMethodDescription(JoinPoint joinPoint)  throws Exception {   
        String targetName = joinPoint.getTarget().getClass().getName();   
        String methodName = joinPoint.getSignature().getName();   
        Object[] arguments = joinPoint.getArgs();   
        Class targetClass = Class.forName(targetName);   
        Method[] methods = targetClass.getMethods();   
        String description = "";   
         for (Method method : methods) {   
             if (method.getName().equals(methodName)) {   
                Class[] clazzs = method.getParameterTypes();   
                 if (clazzs.length == arguments.length) {   
                    description = method.getAnnotation(SystemControllerLog.class).description();   
                     break;   
                }   
            }   
        }   
         return description;   
    }
}

xml 配置文件:<!--通知spring使用cglib而不是jdk的来生成代理方法 AOP可以拦截到Controller--> 
<aop:aspectj-autoproxy proxy-target-class="true" /> 

猜你喜欢

转载自chen-sai-201607223902.iteye.com/blog/2316619