码迷,mamicode.com
首页 > 编程语言 > 详细

spring mvc实现自定义注解

时间:2017-10-29 13:54:58      阅读:392      评论:0      收藏:0      [点我收藏+]

标签:lib   ted   nis   class   shm   logs   time   json.js   context   

实现方式:使用@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();
    }
}

 

spring mvc实现自定义注解

标签:lib   ted   nis   class   shm   logs   time   json.js   context   

原文地址:http://www.cnblogs.com/007sx/p/7749838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!