• spring mvc实现自定义注解


    实现方式:使用@Aspect实现:

    1.

    新建注解接口:CheckSign

    package com.soeasy.web.utils;
    
    import org.springframework.core.Ordered;
    import org.springframework.core.annotation.Order;
    
    import java.lang.annotation.*;
    
    /**
     * Created by Administrator on 2017/10/25 0025.
     */
    @Documented
    @Target({ElementType.METHOD}) //方法上
    @Inherited
    @Retention(RetentionPolicy.RUNTIME) //运行时
    @Order(Ordered.HIGHEST_PRECEDENCE) //最高优先级
    public @interface CheckSign {
    }

    2.

    新建注解实现 CheckSignAspect 类:

    package com.soeasy.web.utils;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.soeasy.commons.FailException;
    import com.soeasy.utils.sign.Des3;
    import com.soeasy.utils.sign.SignConf;
    import org.apache.log4j.Logger;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.stereotype.Component;
    import java.util.HashMap;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    /**
     * 自定义注解@CheckSign的实现方法
     * Created by Administrator on 2017/10/25 0025.
     */
    @Aspect
    @Component
    public class CheckSignAspect {
        private Logger log = Logger.getLogger(this.getClass());
        /**
         * 目标方法执行
         * @param pjp
         */
        @Around("@annotation(com.soeasy.web.utils.CheckSign)") //执行@接口
        public Object beforeExec(ProceedingJoinPoint pjp) throws Throwable {
            //获取request信息
            /*RequestAttributes ra = RequestContextHolder.getRequestAttributes();
            ServletRequestAttributes sra = (ServletRequestAttributes)ra;
            HttpServletRequest request = sra.getRequest();*/
    
            //获取post参数
            for (int i = 0; i < pjp.getArgs().length; i++) {
                if(pjp.getArgs()[i] instanceof LinkedHashMap || pjp.getArgs()[i] instanceof JSONObject){
                    Map<String, Object> map = (Map<String,Object>) pjp.getArgs()[i];
    
                }
            }
            return pjp.proceed();
        }
    }
  • 相关阅读:
    7深入FDOFDO概念
    IP 地址输入控件
    如何在设计时公开复合控件内的子控件
    Hello! CnBlogs... ...
    十句话,我竟然看了好久
    Linux下剪切命令
    Linux下拷贝命令
    Linux下新建文件夹命令
    Linux下*.tar.gz文件解压缩命令
    项目管理四条转载
  • 原文地址:https://www.cnblogs.com/007sx/p/7749838.html
Copyright © 2020-2023  润新知