zoukankan      html  css  js  c++  java
  • 基于Spring的AOP实现自定义annotation操作日志

    引入项目需要的AOP包

     <!--aop-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>4.1.7.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>aspectj</groupId>
          <artifactId>aspectjrt</artifactId>
          <version>1.5.3</version>
        </dependency>
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.8.6</version>
        </dependency>
        <dependency>
          <groupId>aopalliance</groupId>
          <artifactId>aopalliance</artifactId>
          <version>1.0</version>
        </dependency>
        <dependency>
          <groupId>cglib</groupId>
          <artifactId>cglib</artifactId>
          <version>3.2.4</version>
        </dependency>

    要在springmvc的配置文件里加入配置文件,并指明切点类的的包,好被自动扫描

              <aop:aspectj-autoproxy/>
                <context:component-scan base-package="com..util"/>
                <aop:aspectj-autoproxy proxy-target-class="true" />

    首先编写一个annotation的注解类

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

    其次在编写用户管理的切点类,记录用户操作的的时间,ip,操作

    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    import java.lang.reflect.Method;
    import java.util.Date;
    
    
    @Aspect
    @Component
    public class SystemLogAspect {
    
        @Resource
        private EwslogDao ewslogDao;
    
        //Controller层切点,也就是上面那个注解的路径
        @Pointcut("@annotation(com.util.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("user");
            String name ="";
            if(user!=null){
                name = user.getUsername();
            }else
                name = "匿名用户";
            //请求的IP
            String ip = request.getRemoteAddr();
            String description="";
            try {
                description = getControllerMethodDescription(joinPoint);
            } catch (Exception e) {
                e.printStackTrace();
            }
            Ewslog ewslog = new Ewslog();
            ewslog.setLoginName(name);
            ewslog.setLogDate(new Date());
            ewslog.setDescription(description);
            ewslog.setIpAddr(ip);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ip"+name);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ip"+description);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>ip"+ip);
            ewslogDao.save(ewslog);
    
        }
    
        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;
        }
    }

    在需要记录的切面类上只需要添加一个注解就可以完成aop了。spring真是太强大了。

      @RequestMapping("/loginOut")
        @SystemControllerLog(description = "用户登出")
        public String index(HttpServletRequest request){
            HttpSession session = request.getSession();
            session.setAttribute("user", null);
           return "../index";
        }
  • 相关阅读:
    Git命令branch-分支
    Git基础命令(二)
    Git基础命令(一)
    【转载】Android状态栏微技巧,带你真正理解沉浸式模式
    Android性能分析工具
    【转载】Android卡顿检测方案
    WebView loadData中文乱码
    【Cocos2dx】新建场景、场景的切换、设置启动场景与菜单的新建
    Cocos2d-JS切换场景与切换特效
    COCOS2DX场景切换特效
  • 原文地址:https://www.cnblogs.com/yankang/p/6399024.html
Copyright © 2011-2022 走看看