zoukankan      html  css  js  c++  java
  • aop

    package cn.com.rivercloud.log.aspect;
    
    import cn.com.rivercloud.log.domain.SysLogs;
    import cn.com.rivercloud.log.service.SysLogsService;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.shiro.SecurityUtils;
    import org.apache.shiro.subject.Subject;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.util.Map;
    
    /**
     * @author shichangle
     * date 
     */
    
    @Component
    @Aspect
    @Slf4j
    public class LogAspect {
    
        @Autowired
        private SysLogsService sysLogsService;
    
        private long currentTime = 0L;
    
        /**
         * 配置切入点
       * 通过注解
    */ @Pointcut("@annotation(cn.com.rivercloud.log.aop.log.Log)") public void logPointCut(){}
      
        private final String pointCut= "execution(* cn.com....自己写))";
    
    
       /**
         * 配置切入点
       * 通过目录 */

      @Pointcut(pointCut)
        public void logPointCut(){}


    // @Around("logPointCut()") // public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { // Object result; // // currentTime = System.currentTimeMillis(); // result = joinPoint.proceed(); // SysLogs sysLogs = new SysLogs("Info",System.currentTimeMillis()-currentTime); // //存储信息 // String username = getUsername(); // String ip = getIp(); // // sysLogsService.save(username,ip,joinPoint,sysLogs); // return result; // }    @Before(value = "logPointCut()") public void logBefore(JoinPoint joinPoint){ System.out.println("前置通知"); System.out.println(joinPoint); Object[] args = joinPoint.getArgs(); for (Object arg : args) { System.out.println(arg); } } @AfterReturning(value = "logPointCut()",returning = "keys") public void logAfterReturning(JoinPoint joinPoint,Object keys){ Map<String,Object> map = JSON.parseObject(keys.toString()); String success = map.get("success").toString(); if(success.equals("true")){ currentTime = System.currentTimeMillis(); SysLogs sysLogs = new SysLogs("Info",System.currentTimeMillis()-currentTime); //存储信息 String username = getUsername(); String ip = getIp(); sysLogsService.save(username,ip,(ProceedingJoinPoint)joinPoint,sysLogs); }else{ SysLogs log = new SysLogs("ERROR",System.currentTimeMillis()-currentTime); String username = getUsername(); String ip = getIp(); sysLogsService.save(username,ip,(ProceedingJoinPoint) joinPoint,log); } } // /** // * 配置异常通知 // * @param joinPoint // * @param e // */ // @AfterThrowing(pointcut = "logPointCut()",throwing = "e") // public void logAfterThrowing(JoinPoint joinPoint,Throwable e){ // SysLogs log = new SysLogs("ERROR",System.currentTimeMillis()-currentTime); // String username = getUsername(); // String ip = getIp(); // sysLogsService.save(username,ip,(ProceedingJoinPoint) joinPoint,log); // } private String getUsername() { Object principal = SecurityUtils.getSubject().getPrincipal(); return principal.toString(); } private String getIp(){ Subject subject = SecurityUtils.getSubject(); String host = subject.getSession().getHost(); return host; } }
    package cn.com.rivercloud.log.aop.log;
    
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * @author shichangle
     * date
     */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Log {
        String value() default "";
    }
  • 相关阅读:
    java.sql.SQLException: 数字溢出 的解决办法
    oracle数据库创建表,序列及添加代码案例
    Oracle创建用户、角色、授权、建表
    HttpSession与Hibernate中Session的区别
    RuntimeException与CheckedException
    >Hibernate 报错:this project is not a myeclipse hibernate project . assuming hibernate 3 cap
    解决java web项目导入后出现的问题 ---cannot be read or is not a valid ZIP file
    JDK,JRE,JVM区别与联系
    最爱的天籁之音
    applicationContext.xml 基本配置
  • 原文地址:https://www.cnblogs.com/notchangeworld/p/12193502.html
Copyright © 2011-2022 走看看